-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (82 loc) · 2.32 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
package main
import "C"
import (
"github.com/OperatorFoundation/shapeshifter-transports/transports/obfs4/v2"
"github.com/outspace/obfs4-plug-transport/base"
"unsafe"
"net"
"reflect"
)
var obfs4Clients = map[int]*obfs4.Transport{}
var obfs4Connections = map[int]net.Conn{}
var nextID = 0
var obfs4_transport_listener base.TransportListener = nil
//export Initialize_obfs4_c_client
func Initialize_obfs4_c_client(certString *C.char, iatMode int) (clientKey int) {
var err error
goCertString := C.GoString(certString)
obfs4Client, err := obfs4.NewObfs4Client(goCertString, iatMode, nil)
obfs4Clients[nextID] = obfs4Client
if err != nil {
return -1
}
// This is the return value
clientKey = nextID
nextID += 1
return
}
//export Obfs4_listen
func Obfs4_listen(address_string *C.char) {
//goAddressString := C.GoString(address_string)
//obfs4_transport_listener = Obfs4_c_client.Listen(goAddressString)
}
//export Obfs4_dial
func Obfs4_dial(client_id int, address_string *C.char) int {
goAddressString := C.GoString(address_string)
var err error
var transport = obfs4Clients[client_id]
obfs4_transport_connection, err := transport.Dial(goAddressString)
if err != nil {
return -1
}
if obfs4_transport_connection == nil {
return 1
} else {
obfs4Connections[client_id] = obfs4_transport_connection
return 0
}
}
//export Obfs4_write
func Obfs4_write(client_id int, buffer unsafe.Pointer, buffer_length C.int) int {
var connection = obfs4Connections[client_id]
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(client_id int, buffer unsafe.Pointer, buffer_length int) int {
var connection = obfs4Connections[client_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(client_id int) {
var connection = obfs4Connections[client_id]
connection.Close()
delete(obfs4Connections, client_id)
delete(obfs4Clients, client_id)
}
func main() {}