Repository navigation
Expand file tree
/
Copy pathconfig.go
More file actions
108 lines (89 loc) · 1.95 KB
/
Copy pathconfig.go
File metadata and controls
108 lines (89 loc) · 1.95 KB
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
package main
import (
"log"
"strconv"
"strings"
"github.com/go-ini/ini"
)
var (
// from config_manual.go
outboundip = outboundip_const
mpcsz = 256
)
type TunnelRecord struct {
lhost string
lport int
rhost string
rport int
rpubkey string
tname string // tunnel name
tproto string // tunnel protocol: TCP/UDP
}
type TunnelConfig struct {
cfg_file string
recs map[string]*TunnelRecord
srv_name string
sets *ini.File
}
func NewTunnelConfig(cfg_file string) *TunnelConfig {
f, err := ini.Load(cfg_file)
if err != nil {
log.Println(lerrorp, err)
return nil
}
recs := make(map[string]*TunnelRecord, 0)
srv_val, err := f.Section("server").GetKey("name")
srv_name := srv_val.String()
for _, key := range f.Section("client").KeyStrings() {
cli_val, err := f.Section("client").GetKey(key)
if err != nil {
}
line := cli_val.String()
rec := parseRecordLine(line)
rec.tname = key
if _, ok := recs[key]; ok {
// already exist
}
recs[key] = &rec
}
return &TunnelConfig{cfg_file, recs, srv_name, f}
}
func parseRecordLine(line string) TunnelRecord {
sep := ":"
segs := strings.Split(line, sep)
tproto := strings.ToLower(segs[0])
lhost := segs[1]
lport, err := strconv.Atoi(segs[2])
if err != nil {
}
rhost := segs[3]
rport, err := strconv.Atoi(segs[4])
rpubkey := segs[5]
if len(rpubkey) != 76 {
}
return TunnelRecord{
lhost, lport, rhost, rport, rpubkey, "", tproto,
}
}
func friendInConfig(pubkey string) bool {
for _, rec := range config.recs {
if pubkey == rec.rpubkey || strings.HasPrefix(rec.rpubkey, pubkey) {
return true
}
}
return false
}
func (this *TunnelConfig) friendInConfig(pubkey string) bool {
for _, rec := range config.recs {
if pubkey == rec.rpubkey || strings.HasPrefix(rec.rpubkey, pubkey) {
return true
}
}
return false
}
func (this *TunnelConfig) getRecordByName(tname string) *TunnelRecord {
if rec, ok := this.recs[tname]; ok {
return rec
}
return nil
}