WIP: cleanup. Local peer discovery working.
This commit is contained in:
		
							
								
								
									
										103
									
								
								hub/api/api.go
									
									
									
									
									
								
							
							
						
						
									
										103
									
								
								hub/api/api.go
									
									
									
									
									
								
							| @@ -24,7 +24,6 @@ var migrations embed.FS | ||||
| type API struct { | ||||
| 	db          *sql.DB | ||||
| 	lock        sync.Mutex | ||||
| 	peerIntents map[string]PeerCreateArgs | ||||
| 	initIntents map[string]byte // Map from intent key to peer IP | ||||
| } | ||||
|  | ||||
| @@ -40,7 +39,6 @@ func New(dbPath string) (*API, error) { | ||||
|  | ||||
| 	a := &API{ | ||||
| 		db:          sqlDB, | ||||
| 		peerIntents: map[string]PeerCreateArgs{}, | ||||
| 		initIntents: map[string]byte{}, | ||||
| 	} | ||||
|  | ||||
| @@ -153,34 +151,6 @@ func (a *API) Peer_CreateNew(p *Peer) error { | ||||
| 	return db.Peer_Insert(a.db, p) | ||||
| } | ||||
|  | ||||
| // TODO: Remove | ||||
| type PeerCreateArgs struct { | ||||
| 	Name     string | ||||
| 	PublicIP []byte | ||||
| 	Port     uint16 | ||||
| 	Relay    bool | ||||
| } | ||||
|  | ||||
| // TODO: Remove | ||||
| // Create the intention to add a peer. The returned code is used to complete | ||||
| // the peer creation. The code is valid for 5 minutes. | ||||
| func (a *API) Peer_CreateIntent(args PeerCreateArgs) string { | ||||
| 	a.lock.Lock() | ||||
| 	defer a.lock.Unlock() | ||||
|  | ||||
| 	code := idgen.NewToken() | ||||
| 	a.peerIntents[code] = args | ||||
|  | ||||
| 	go func() { | ||||
| 		time.Sleep(5 * time.Minute) | ||||
| 		a.lock.Lock() | ||||
| 		defer a.lock.Unlock() | ||||
| 		delete(a.peerIntents, code) | ||||
| 	}() | ||||
|  | ||||
| 	return code | ||||
| } | ||||
|  | ||||
| // Create the intention to initialize a peer. The returned code is used to | ||||
| // complete the peer initialization. The code is valid for 5 minutes. | ||||
| func (a *API) Peer_CreateInitIntent(peerIP byte) string { | ||||
| @@ -252,79 +222,6 @@ func (a *API) Peer_Init(initCode string) (*m.PeerConfig, error) { | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| // TODO: Remove | ||||
| func (a *API) Peer_Create(creationCode string) (*m.PeerConfig, error) { | ||||
| 	a.lock.Lock() | ||||
| 	defer a.lock.Unlock() | ||||
|  | ||||
| 	args, ok := a.peerIntents[creationCode] | ||||
| 	if !ok { | ||||
| 		return nil, ErrNotAuthorized | ||||
| 	} | ||||
|  | ||||
| 	delete(a.peerIntents, creationCode) | ||||
|  | ||||
| 	encPubKey, encPrivKey, err := box.GenerateKey(rand.Reader) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	signPubKey, signPrivKey, err := sign.GenerateKey(rand.Reader) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	// Get peer IP. | ||||
| 	peerIP := byte(0) | ||||
|  | ||||
| 	for i := byte(1); i < 255; i++ { | ||||
| 		exists, err := db.Peer_Exists(a.db, i) | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		if !exists { | ||||
| 			peerIP = i | ||||
| 			break | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if peerIP == 0 { | ||||
| 		return nil, ErrNoIPAvailable | ||||
| 	} | ||||
|  | ||||
| 	peer := &Peer{ | ||||
| 		PeerIP:     peerIP, | ||||
| 		Version:    idgen.NextID(0), | ||||
| 		APIKey:     idgen.NewToken(), | ||||
| 		Name:       args.Name, | ||||
| 		PublicIP:   args.PublicIP, | ||||
| 		Port:       args.Port, | ||||
| 		Relay:      args.Relay, | ||||
| 		PubKey:     encPubKey[:], | ||||
| 		PubSignKey: signPubKey[:], | ||||
| 	} | ||||
|  | ||||
| 	if err := db.Peer_Insert(a.db, peer); err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
|  | ||||
| 	conf := a.Config_Get() | ||||
|  | ||||
| 	return &m.PeerConfig{ | ||||
| 		PeerIP:      peer.PeerIP, | ||||
| 		HubAddress:  conf.HubAddress, | ||||
| 		APIKey:      peer.APIKey, | ||||
| 		Network:     conf.VPNNetwork, | ||||
| 		PublicIP:    peer.PublicIP, | ||||
| 		Port:        peer.Port, | ||||
| 		Relay:       peer.Relay, | ||||
| 		PubKey:      encPubKey[:], | ||||
| 		PrivKey:     encPrivKey[:], | ||||
| 		PubSignKey:  signPubKey[:], | ||||
| 		PrivSignKey: signPrivKey[:], | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (a *API) Peer_Update(p *Peer) error { | ||||
| 	a.lock.Lock() | ||||
| 	defer a.lock.Unlock() | ||||
|   | ||||
| @@ -1 +0,0 @@ | ||||
| package api | ||||
		Reference in New Issue
	
	Block a user