-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathprotocol.go
More file actions
93 lines (77 loc) · 2.08 KB
/
Copy pathprotocol.go
File metadata and controls
93 lines (77 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package protocol
import (
"encoding/binary"
"encoding/json"
"fmt"
"io"
"github.com/libp2p/go-libp2p/core/protocol"
)
const (
version = "0.3.0"
basePath = "/awl/" + version
AuthMethod protocol.ID = basePath + "/auth/"
GetStatusMethod protocol.ID = basePath + "/status/"
TunnelPacketMethod protocol.ID = basePath + "/tunnel/"
Socks5PacketMethod protocol.ID = basePath + "/socks5/"
Socks5NoAuthMethod protocol.ID = basePath + "/socks5-noauth/"
)
type (
PeerStatusInfo struct {
Name string
Declined bool
AllowUsingAsExitNode bool
}
)
func ReceiveStatus(stream io.Reader) (PeerStatusInfo, error) {
statusInfo := PeerStatusInfo{}
err := json.NewDecoder(stream).Decode(&statusInfo)
return statusInfo, err
}
func SendStatus(stream io.Writer, statusInfo PeerStatusInfo) error {
err := json.NewEncoder(stream).Encode(&statusInfo)
return err
}
type AuthPeer struct {
Name string
}
type AuthPeerResponse struct {
Confirmed bool
Declined bool
}
func ReceiveAuth(stream io.Reader) (AuthPeer, error) {
authPeer := AuthPeer{}
err := json.NewDecoder(stream).Decode(&authPeer)
return authPeer, err
}
func SendAuth(stream io.Writer, authPeer AuthPeer) error {
err := json.NewEncoder(stream).Encode(&authPeer)
return err
}
func ReceiveAuthResponse(stream io.Reader) (AuthPeerResponse, error) {
response := AuthPeerResponse{}
err := json.NewDecoder(stream).Decode(&response)
return response, err
}
func SendAuthResponse(stream io.Writer, response AuthPeerResponse) error {
err := json.NewEncoder(stream).Encode(&response)
return err
}
func ReadUint64(stream io.Reader) (uint64, error) {
var data [8]byte
n, err := io.ReadFull(stream, data[:])
if err != nil {
return 0, err
}
if n != 8 {
return 0, fmt.Errorf("invalid uint64 data: %v. read %d instead of 8", data, n)
}
value := binary.BigEndian.Uint64(data[:])
return value, nil
}
func AppendPacketToBuf(buf, packet []byte) []byte {
var lenHeader [8]byte
binary.BigEndian.PutUint64(lenHeader[:], uint64(len(packet)))
buf = append(buf, lenHeader[:]...)
buf = append(buf, packet...)
return buf
}