-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsession_config.go
158 lines (146 loc) · 4.35 KB
/
session_config.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
package go_i2cp
import (
"bufio"
"os"
"regexp"
"time"
)
type SessionConfigProperty int
const (
SESSION_CONFIG_PROP_CRYPTO_LOW_TAG_THRESHOLD SessionConfigProperty = iota
SESSION_CONFIG_PROP_CRYPTO_TAGS_TO_SEND
SESSION_CONFIG_PROP_I2CP_DONT_PUBLISH_LEASE_SET
SESSION_CONFIG_PROP_I2CP_FAST_RECEIVE
SESSION_CONFIG_PROP_I2CP_GZIP
SESSION_CONFIG_PROP_I2CP_MESSAGE_RELIABILITY
SESSION_CONFIG_PROP_I2CP_PASSWORD
SESSION_CONFIG_PROP_I2CP_USERNAME
SESSION_CONFIG_PROP_INBOUND_ALLOW_ZERO_HOP
SESSION_CONFIG_PROP_INBOUND_BACKUP_QUANTITY
SESSION_CONFIG_PROP_INBOUND_IP_RESTRICTION
SESSION_CONFIG_PROP_INBOUND_LENGTH
SESSION_CONFIG_PROP_INBOUND_LENGTH_VARIANCE
SESSION_CONFIG_PROP_INBOUND_NICKNAME
SESSION_CONFIG_PROP_INBOUND_QUANTITY
SESSION_CONFIG_PROP_OUTBOUND_ALLOW_ZERO_HOP
SESSION_CONFIG_PROP_OUTBOUND_BACKUP_QUANTITY
SESSION_CONFIG_PROP_OUTBOUND_IP_RESTRICTION
SESSION_CONFIG_PROP_OUTBOUND_LENGTH
SESSION_CONFIG_PROP_OUTBOUND_LENGTH_VARIANCE
SESSION_CONFIG_PROP_OUTBOUND_NICKNAME
SESSION_CONFIG_PROP_OUTBOUND_PRIORITY
SESSION_CONFIG_PROP_OUTBOUND_QUANTITY
NR_OF_SESSION_CONFIG_PROPERTIES
)
var sessionOptions = [NR_OF_SESSION_CONFIG_PROPERTIES]string{
"crypto.lowTagThreshold",
"crypto.tagsToSend",
"i2cp.dontPublishLeaseSet",
"i2cp.fastReceive",
"i2cp.gzip",
"i2cp.messageReliability",
"i2cp.password",
"i2cp.username",
"inbound.allowZeroHop",
"inbound.backupQuantity",
"inbound.IPRestriction",
"inbound.length",
"inbound.lengthVariance",
"inbound.nickname",
"inbound.quantity",
"outbound.allowZeroHop",
"outbound.backupQuantity",
"outbound.IPRestriction",
"outbound.length",
"outbound.lengthVariance",
"outbound.nickname",
"outbound.priority",
"outbound.quantity",
}
var configRegex = regexp.MustCompile("\\s*([\\w.]+)=\\s*(.+)\\s*;\\s*")
type SessionConfig struct {
properties [NR_OF_SESSION_CONFIG_PROPERTIES]string
date uint64
destination *Destination
}
func NewSessionConfigFromDestinationFile(filename string) (config SessionConfig) {
var home string
if file, err := os.Open(filename); err == nil {
config.destination, err = NewDestinationFromFile(file)
if err != nil {
Warning(SESSION_CONFIG, "Failed to load destination from file '%s', a new destination will be generated.", filename)
}
}
if config.destination == nil {
config.destination, _ = NewDestination()
}
if len(filename) > 0 {
config.destination.WriteToFile(filename)
}
home = os.Getenv("HOME")
if len(home) > 0 {
configFile := home + "/.i2cp.conf"
ParseConfig(configFile, func(name, value string) {
if prop := config.propFromString(name); prop >= 0 {
config.SetProperty(prop, value)
}
})
}
return config
}
func (config *SessionConfig) writeToMessage(stream *Stream) {
config.destination.WriteToMessage(stream)
config.writeMappingToMessage(stream)
stream.WriteUint64(uint64(time.Now().Unix() * 1000))
GetCryptoInstance().WriteSignatureToStream(&config.destination.sgk, stream)
}
func (config *SessionConfig) writeMappingToMessage(stream *Stream) (err error) {
m := make(map[string]string)
for i := 0; i < int(NR_OF_SESSION_CONFIG_PROPERTIES); i++ {
var option string
if config.properties[i] == "" {
continue
}
option = config.configOptLookup(SessionConfigProperty(i))
if option == "" {
continue
}
m[option] = config.properties[i]
}
Debug(SESSION_CONFIG, "Writing %d options to mapping table", len(m))
return stream.WriteMapping(m)
}
func (config *SessionConfig) configOptLookup(property SessionConfigProperty) string {
return sessionOptions[property]
}
func (config *SessionConfig) propFromString(name string) SessionConfigProperty {
for i := 0; SessionConfigProperty(i) < NR_OF_SESSION_CONFIG_PROPERTIES; i++ {
if sessionOptions[i] == name {
return SessionConfigProperty(i)
}
}
return SessionConfigProperty(-1)
}
func (config *SessionConfig) SetProperty(prop SessionConfigProperty, value string) {
config.properties[prop] = value
}
func ParseConfig(s string, cb func(string, string)) {
file, err := os.Open(s)
if err != nil {
Error(SESSION_CONFIG, err.Error())
return
}
Debug(SESSION_CONFIG, "Parsing config file '%s'", s)
scan := bufio.NewScanner(file)
for scan.Scan() {
line := scan.Text()
groups := configRegex.FindStringSubmatch(line)
if len(groups) != 3 {
continue
}
cb(groups[1], groups[2])
}
if err := scan.Err(); err != nil {
Error(SESSION_CONFIG, "reading input from %s config %s", s, err.Error())
}
}