@@ -54,37 +54,50 @@ func (ca *ClientAuth) AuthKey() string {
54
54
}
55
55
56
56
func (ca * ClientAuth ) Parse (authKey string ) error {
57
- dec := cursor {
58
- b : base58 .Decode (authKey ),
59
- }
60
-
61
57
if len (authKey ) == 0 {
62
58
return errors .New ("auth key should not be empty" )
63
59
}
64
60
65
- ipLen := int (dec .next (1 )[0 ])
61
+ decr := bytes .NewReader (base58 .Decode (authKey ))
62
+
63
+ ipLenB , err := decr .ReadByte ()
64
+ if err != nil {
65
+ return errors .New ("read STUN ip len; invalid authkey" )
66
+ }
67
+
68
+ ipLen := int (ipLenB )
66
69
if ipLen > 0 {
67
- stunIPBytes := dec .next (ipLen + 2 )
68
- err := ca .ReceiverStunAddr .UnmarshalBinary (stunIPBytes )
70
+ stunIPBytes := make ([]byte , ipLen + 2 )
71
+ n , err := decr .Read (stunIPBytes )
72
+ if n != len (stunIPBytes ) || err != nil {
73
+ return errors .New ("read STUN ip; invalid authkey" )
74
+ }
75
+
76
+ err = ca .ReceiverStunAddr .UnmarshalBinary (stunIPBytes )
69
77
if err != nil {
70
78
return fmt .Errorf ("unmarshal receiver stun address: %w" , err )
71
79
}
72
80
}
73
81
74
- ca . ReceiverDERPRegionID = binary . BigEndian . Uint16 ( dec . next ( 2 ) )
75
-
76
- ca . ReceiverPublicKey = key . NodePublicFromRaw32 ( mem . B ( dec . next ( 32 )))
77
- ca . OverlayPrivateKey = key . NodePrivateFromRaw32 ( mem . B ( dec . next ( 32 )) )
78
- return nil
79
- }
82
+ derpRegionBytes := make ([] byte , 2 )
83
+ n , err := decr . Read ( derpRegionBytes )
84
+ if n != len ( derpRegionBytes ) || err != nil {
85
+ return errors . New ( "read derp region; invalid authkey" )
86
+ }
87
+ ca . ReceiverDERPRegionID = binary . BigEndian . Uint16 ( derpRegionBytes )
80
88
81
- type cursor struct {
82
- at int
83
- b []byte
84
- }
89
+ pubKeyBytes := make ([]byte , 32 )
90
+ n , err = decr .Read (pubKeyBytes )
91
+ if n != len (pubKeyBytes ) || err != nil {
92
+ return errors .New ("read receiver pubkey; invalid authkey" )
93
+ }
94
+ ca .ReceiverPublicKey = key .NodePublicFromRaw32 (mem .B (pubKeyBytes ))
85
95
86
- func (c * cursor ) next (i int ) []byte {
87
- ret := c .b [c .at : c .at + i ]
88
- c .at += i
89
- return ret
96
+ privKeyBytes := make ([]byte , 32 )
97
+ n , err = decr .Read (privKeyBytes )
98
+ if n != len (privKeyBytes ) || err != nil {
99
+ return errors .New ("read overlay privkey; invalid authkey" )
100
+ }
101
+ ca .OverlayPrivateKey = key .NodePrivateFromRaw32 (mem .B (privKeyBytes ))
102
+ return nil
90
103
}
0 commit comments