MCPcopy Create free account
hub / github.com/0xUnixIO/pulse / syncUserInbounds

Method syncUserInbounds

internal/serverapi/users.go:746–802  ·  view source on GitHub ↗

syncUserInbounds reconciles a user's inbound assignments. Returns affected node IDs.

(userID string, selectedIDs []string)

Source from the content-addressed store, hash-verified

744
745// syncUserInbounds reconciles a user's inbound assignments. Returns affected node IDs.
746func (a *userAPI) syncUserInbounds(userID string, selectedIDs []string) ([]string, error) {
747 wantedInbounds := make(map[string]inbounds.Inbound)
748 for _, ibID := range selectedIDs {
749 ib, err := a.inboundStore.GetInbound(ibID)
750 if err != nil {
751 continue
752 }
753 wantedInbounds[ibID] = ib
754 }
755
756 existing, err := a.users.ListUserInboundsByUser(userID)
757 if err != nil {
758 return nil, err
759 }
760 existingByInbound := make(map[string]users.UserInbound, len(existing))
761 for _, acc := range existing {
762 existingByInbound[acc.InboundID] = acc
763 }
764
765 changedNodeIDs := make(map[string]struct{})
766
767 for ibID, ib := range wantedInbounds {
768 if _, ok := existingByInbound[ibID]; !ok {
769 secret := randomToken(12)
770 if ib.Protocol == "shadowsocks" && strings.HasPrefix(ib.Method, "2022-") {
771 secret = ssPassword(ib.Method)
772 }
773 acc := users.UserInbound{
774 ID: idgen.NextString(),
775 UserID: userID,
776 InboundID: ibID,
777 NodeID: ib.NodeID,
778 UUID: randomUUID(),
779 Secret: secret,
780 }
781 if _, err := a.users.UpsertUserInbound(acc); err != nil {
782 return nil, err
783 }
784 changedNodeIDs[ib.NodeID] = struct{}{}
785 }
786 }
787
788 for ibID, acc := range existingByInbound {
789 if _, wanted := wantedInbounds[ibID]; !wanted {
790 if err := a.users.DeleteUserInbound(acc.ID); err != nil {
791 return nil, err
792 }
793 changedNodeIDs[acc.NodeID] = struct{}{}
794 }
795 }
796
797 affected := make([]string, 0, len(changedNodeIDs))
798 for id := range changedNodeIDs {
799 affected = append(affected, id)
800 }
801 return affected, nil
802}
803

Callers 2

handleUsersMethod · 0.95
handleUpdateUserMethod · 0.95

Calls 8

NextStringFunction · 0.92
ssPasswordFunction · 0.85
randomUUIDFunction · 0.85
randomTokenFunction · 0.70
GetInboundMethod · 0.65
UpsertUserInboundMethod · 0.65
DeleteUserInboundMethod · 0.65

Tested by

no test coverage detected