This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
188 lines (147 loc) · 3.7 KB
/
main.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
// #include <stdint.h>
// void call_fd_callback(void* func_ptr, void* class_ptr, uintptr_t fd);
import "C"
import (
"net"
"unsafe"
"reflect"
"github.com/TunnelBear/shapeshifter-transports/transports/obfs4/v2"
)
var transports = map[int]*obfs4.Transport{}
var listeners = map[int]net.Listener{}
var conns = map[int]net.Conn{}
var tcpConns = map[int]*net.TCPConn{} // store connections to get RawConn for the file descriptor
var nextID = 0
//export Obfs4_initialize_server
func Obfs4_initialize_server(stateDir *C.char) (listenerKey int) {
goStateString := C.GoString(stateDir)
transport, _ := obfs4.NewObfs4Server(goStateString)
transports[nextID] = transport
// This is the return value
if transport != nil {
listenerKey = nextID
} else {
listenerKey = -1
}
nextID += 1
return
}
//export Obfs4_initialize_client
func Obfs4_initialize_client(cert *C.char, iatMode int) (listenerKey int) {
certString := C.GoString(cert)
transport, _ := obfs4.NewObfs4Client(certString, iatMode, tcpDialer{nextID})
transports[nextID] = transport
// This is the return value
if transport != nil {
listenerKey = nextID
} else {
listenerKey = -1
}
nextID += 1
return
}
//export Obfs4_listen
func Obfs4_listen(id int, address_string *C.char) {
goAddressString := C.GoString(address_string)
var transport = transports[id]
var listener = transport.Listen(goAddressString)
listeners[id] = listener
}
//export Obfs4_dial
func Obfs4_dial(id int, address_string *C.char) int {
goAddressString := C.GoString(address_string)
var transport = transports[id]
var conn, err = transport.Dial(goAddressString)
if err != nil {
return -1
} else {
conns[id] = conn
return 0
}
}
//export Obfs4_accept
func Obfs4_accept(id int) {
var listener = listeners[id]
conn, err := listener.Accept()
if err != nil {
return
}
conns[id] = conn
}
//export Obfs4_write
func Obfs4_write(listener_id int, buffer unsafe.Pointer, buffer_length C.int) int {
var connection = conns[listener_id]
if connection == nil {
return -1
}
var bytesBuffer = C.GoBytes(buffer, buffer_length)
numberOfBytesWritten, error := connection.Write(bytesBuffer)
if error != nil {
return -1
} else {
return numberOfBytesWritten
}
}
//export Obfs4_read
func Obfs4_read(listener_id int, buffer unsafe.Pointer, buffer_length int) int {
var connection = conns[listener_id]
if connection == nil {
return -1
}
header := reflect.SliceHeader{uintptr(buffer), buffer_length, buffer_length}
bytesBuffer := *(*[]byte)(unsafe.Pointer(&header))
numberOfBytesRead, error := connection.Read(bytesBuffer)
if error != nil {
return -1
} else {
return numberOfBytesRead
}
}
//export Obfs4_close_connection
func Obfs4_close_connection(listener_id int) {
var connection = conns[listener_id]
if connection == nil {
return
}
connection.Close()
delete(conns, listener_id)
}
//export Obfs4_get_fd
func Obfs4_get_fd(listener_id int, function unsafe.Pointer, class_ptr unsafe.Pointer) int {
if function == nil {
return -1
}
conn := tcpConns[listener_id]
if conn == nil {
return -1
}
rawConn, error := conn.SyscallConn()
if error != nil {
return -1
}
error = rawConn.Control(func (fd uintptr) {
C.call_fd_callback(function, class_ptr, C.uintptr_t(fd))
})
if error != nil {
return -1
}
return 0
}
// custom dialer to store the tcpConn + get the file descriptor
type tcpDialer struct {
id int // transport id
}
func (d tcpDialer) Dial(network, addr string) (c net.Conn, err error) {
tcpAddr, error := net.ResolveTCPAddr(network, addr)
if error != nil {
return nil, error
}
conn, error := net.DialTCP(network, nil, tcpAddr)
if error != nil {
return nil, error
}
tcpConns[d.id] = conn
return conn, nil
}
func main() {}