-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_lib.go
191 lines (162 loc) · 3.59 KB
/
main_lib.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
189
190
191
// +build lib
package main
/*
#include <stdio.h>
#include <stdlib.h>
#include "Away.h"
static inline char** away_rules_alloc(int count) {
return malloc(count * sizeof(char*));
}
static inline void away_rules_set(char **rs, int index, char *r) {
rs[index] = r;
}
*/
import "C"
import (
"log/syslog"
"os"
"os/signal"
"path"
"syscall"
"unsafe"
log "github.com/sirupsen/logrus"
slog "github.com/sirupsen/logrus/hooks/syslog"
)
func main() {
}
const AppName = "Away"
var away *Away
var dataPath string
//export away_initialize
func away_initialize(dir *C.char) C.int {
signal.Ignore(syscall.SIGPIPE) // https://github.com/golang/go/issues/17393
log.SetFormatter(&log.TextFormatter{FullTimestamp: true, DisableColors: true, TimestampFormat: "2006/01/02 15:04:05.000"})
if h, err := slog.NewSyslogHook("", "", syslog.LOG_INFO, AppName); err == nil {
log.AddHook(h)
}
p := C.GoString(dir)
if err := os.MkdirAll(p, os.ModePerm); err != nil {
log.Error(err)
return -1
}
dataPath = p
rulefile := path.Join(dataPath, "rules")
away = NewAway(ModeRule, rulefile)
n, err := away.LoadRules()
if err != nil {
log.Error(err)
return -1
} else {
log.Infof("Initilize [%d] rules.", n)
}
return 0
}
var socksSrv *SocksSrv
func startSocksSrv(s *Settings, a *Away) error {
if socksSrv != nil {
socksSrv.Stop()
}
srv, err := NewSocksSrv(s, a)
if err != nil {
return err
}
socksSrv = srv
go socksSrv.Start()
return nil
}
//export away_start
func away_start() C.int {
s, err := ReadSettings(settingsFilename())
if err != nil {
log.Error(err)
return -1
}
if err := startSocksSrv(s, away); err != nil {
log.Error(err)
return -1
}
return 0
}
func settingsFilename() string {
return path.Join(dataPath, "settings")
}
//export away_settings_exist
func away_settings_exist() C.int {
if ExistSetting(settingsFilename()) {
return 1
}
return 0
}
//export away_settings_get
func away_settings_get(s *C.struct_settings) {
if rs, err := ReadSettings(settingsFilename()); err == nil {
s.remote = C.CString(rs.Remote)
s.passkey = C.CString(rs.Passkey)
s.port = C.CString(rs.Port)
}
}
//export away_settings_change
func away_settings_change(s C.struct_settings) C.int {
st := &Settings{
Remote: C.GoString(s.remote),
Passkey: C.GoString(s.passkey),
Port: C.GoString(s.port),
}
if err := WriteSettings(st, settingsFilename()); err != nil {
log.Errorln(err)
return -1
}
if err := startSocksSrv(st, away); err != nil {
log.Error(err)
return -1
}
return 0
}
//export away_settings_free
func away_settings_free(s *C.struct_settings) {
C.free(unsafe.Pointer(s.remote))
C.free(unsafe.Pointer(s.passkey))
C.free(unsafe.Pointer(s.port))
}
//export away_rule_add
func away_rule_add(r *C.char) C.int {
rule := C.GoString(r)
if err := away.AddRule(rule); err != nil {
log.Errorf("fail to add rule %s, %s", rule, err)
return -1
}
if err := away.WriteRules(); err != nil {
log.Errorf("fail to add rule %s, %s", rule, err)
return -1
}
return 0
}
//export away_rule_del
func away_rule_del(r *C.char) C.int {
rule := C.GoString(r)
away.DeleteRule(rule)
if err := away.WriteRules(); err != nil {
log.Errorf("fail to delete rule %s, %s", rule, err)
return -1
}
return 0
}
//export away_rules_get
func away_rules_get() **C.char {
rules := away.SortRules()
l := len(rules)
if l == 0 {
return nil
}
rs := C.away_rules_alloc(C.int(l + 1))
for i, r := range rules {
C.away_rules_set(rs, C.int(i), C.CString(r))
}
C.away_rules_set(rs, C.int(l), nil)
return rs
}
//export away_mode_change
func away_mode_change(m C.enum_away_mode) C.int {
away.ChangeMode(rune(m))
return 0
}