/
cs.go
125 lines (100 loc) · 2.32 KB
/
cs.go
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package proto
import (
"encoding/binary"
"encoding/json"
"io"
"net"
)
const (
CmdAuth = iota
CmdHeartbeat
CmdData
)
type S2CHeartbeat struct {
}
type C2SHeartbeat struct{}
type C2SAuth struct {
Key string `json:"key"`
Domain string `json:"domain"`
Forward []ForwardItem `json:"forwards"`
}
type ForwardItem struct {
// forward protocol. eg: tcp, udp, https, http
Protocol string `json:"protocol"`
// forward ports
// key is the port opennotrd listen
// value is local port
Ports map[int]string `json:"ports"`
// local ip, default is 127.0.0.1
// the traffic will be forward to $LocalIP:$LocalPort
// for example: 127.0.0.1:8080. 192.168.31.65:8080
LocalIP string `json:"localIP"`
// raw config pass to server
RawConfig string `json:"rawConfig"`
}
type S2CAuth struct {
Domain string `json:"domain"` // 分配域名
Vip string `json:"vip"` // 分配虚拟ip地址
}
type ProxyProtocol struct {
Protocol string `json:"protocol"`
SrcIP string `json:"sip"`
SrcPort string `json:"sport"`
DstIP string `json:"dip"`
DstPort string `json:"dport"`
}
// 1字节版本
// 1字节命令
// 2字节长度
type Header [4]byte
func (h Header) Version() int {
return int(h[0])
}
func (h Header) Cmd() int {
return int(h[1])
}
func (h Header) Bodylen() int {
return (int(h[2]) << 8) + int(h[3])
}
func Read(conn net.Conn) (Header, []byte, error) {
h := Header{}
_, err := io.ReadFull(conn, h[:])
if err != nil {
return h, nil, err
}
bodylen := h.Bodylen()
if bodylen <= 0 {
return h, nil, nil
}
body := make([]byte, bodylen)
_, err = io.ReadFull(conn, body)
if err != nil {
return h, nil, err
}
return h, body, nil
}
func Write(conn net.Conn, cmd int, body []byte) error {
bodylen := make([]byte, 2)
binary.BigEndian.PutUint16(bodylen, uint16(len(body)))
hdr := []byte{0x01, byte(cmd)}
hdr = append(hdr, bodylen...)
writebody := make([]byte, 0)
writebody = append(writebody, hdr...)
writebody = append(writebody, body...)
_, err := conn.Write(writebody)
return err
}
func WriteJSON(conn net.Conn, cmd int, obj interface{}) error {
body, err := json.Marshal(obj)
if err != nil {
return err
}
return Write(conn, cmd, body)
}
func ReadJSON(conn net.Conn, obj interface{}) error {
_, body, err := Read(conn)
if err != nil {
return err
}
return json.Unmarshal(body, obj)
}