This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
addr.go
148 lines (137 loc) · 3.04 KB
/
addr.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package trojanc
import (
"encoding/binary"
"fmt"
"io"
"net"
"github.com/daeuniverse/softwind/pool"
"github.com/daeuniverse/softwind/protocol"
)
func ParseMetadataType(t byte) protocol.MetadataType {
switch t {
case 1:
return protocol.MetadataTypeIPv4
case 2:
return protocol.MetadataTypeMsg
case 3:
return protocol.MetadataTypeDomain
case 4:
return protocol.MetadataTypeIPv6
default:
return protocol.MetadataTypeInvalid
}
}
func MetadataTypeToByte(typ protocol.MetadataType) byte {
switch typ {
case protocol.MetadataTypeIPv4:
return 1
case protocol.MetadataTypeMsg:
return 2
case protocol.MetadataTypeDomain:
return 3
case protocol.MetadataTypeIPv6:
return 4
default:
return 0
}
}
func ParseNetwork(n byte) string {
switch n {
case 1:
return "tcp"
case 3:
return "udp"
default:
return "invalid"
}
}
func NetworkToByte(network string) byte {
switch network {
case "tcp":
return 1
case "udp":
return 3
default:
return 0
}
}
type Metadata struct {
protocol.Metadata
Network string
}
var (
ErrInvalidMetadata = fmt.Errorf("invalid metadata")
)
func (m *Metadata) Len() int {
switch m.Type {
case protocol.MetadataTypeIPv4:
return 3 + 4
case protocol.MetadataTypeIPv6:
return 3 + 16
case protocol.MetadataTypeDomain:
return 4 + len([]byte(m.Hostname))
case protocol.MetadataTypeMsg:
return 2
default:
return 0
}
}
func (m *Metadata) PackTo(dst []byte) (n int) {
dst[0] = MetadataTypeToByte(m.Type)
switch m.Type {
case protocol.MetadataTypeIPv4:
copy(dst[1:], net.ParseIP(m.Hostname).To4()[:4])
binary.BigEndian.PutUint16(dst[5:], m.Port)
return 7
case protocol.MetadataTypeIPv6:
copy(dst[1:], net.ParseIP(m.Hostname)[:16])
binary.BigEndian.PutUint16(dst[17:], m.Port)
return 19
case protocol.MetadataTypeDomain:
dst[1] = byte(len([]byte(m.Hostname)))
copy(dst[2:], m.Hostname)
binary.BigEndian.PutUint16(dst[2+dst[1]:], m.Port)
return 4 + int(dst[1])
case protocol.MetadataTypeMsg:
dst[1] = byte(m.Cmd)
return 2
default:
return 0
}
}
func (m *Metadata) Unpack(r io.Reader) (n int, err error) {
buf := pool.Get(256)
defer buf.Put()
if _, err = io.ReadFull(r, buf[:2]); err != nil {
return 0, err
}
m.Type = ParseMetadataType(buf[0])
switch m.Type {
case protocol.MetadataTypeIPv4:
if _, err = io.ReadFull(r, buf[2:7]); err != nil {
return 0, err
}
m.Hostname = net.IP(buf[1:5]).String()
m.Port = binary.BigEndian.Uint16(buf[5:])
return 7, nil
case protocol.MetadataTypeIPv6:
if _, err = io.ReadFull(r, buf[2:19]); err != nil {
return 0, err
}
m.Hostname = net.IP(buf[1:17]).String()
m.Port = binary.BigEndian.Uint16(buf[17:])
return 19, nil
case protocol.MetadataTypeDomain:
if _, err = io.ReadFull(r, buf[2:4+int(buf[1])]); err != nil {
return 0, err
}
m.Hostname = string(buf[2 : 2+buf[1]])
m.Port = binary.BigEndian.Uint16(buf[2+buf[1]:])
return 4 + int(buf[1]), nil
case protocol.MetadataTypeMsg:
m.Cmd = protocol.MetadataCmd(buf[1])
return 2, nil
default:
return 0, fmt.Errorf("unexpected metadata type: %v", m.Type)
}
}