-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmessage.go
165 lines (143 loc) · 4.31 KB
/
message.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
/*
* TVPN: A Peer-to-Peer VPN solution for traversing NAT firewalls
* Copyright (C) 2013 Joshua Chase <jcjoshuachase@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package tvpn
import (
"net"
"encoding/base64"
"fmt"
"math/big"
"regexp"
"strconv"
)
type Message struct {
To, From string
Type int
Data map[string]string
}
const (
Init int = iota
Join
Quit
Accept
Deny
Dhpub
Tunnip
Conninfo
Reset
)
type messageError struct {
message string
}
func (e messageError) Error() string {
return e.message
}
const (
initRE string = `^INIT$`
acceptRE = `^ACCEPT$`
denyRE = `^DENY (?P<reason>.*)$`
dhpubRE = `^DHPUB (?P<i>[0-3]) (?P<x>[A-Za-z0-9+/=]+) (?P<y>[A-Za-z0-9+/=]+)$`
tunnipRE = `^TUNNIP (?P<ip>[0-9]{1,3}(?:\.[0-9]{1,3}){3})$`
conninfoRE = `^CONNINFO (?P<ip>[0-9]{1,3}(?:\.[0-9]{1,3}){3}) (?P<port>[0-9]+)$`
resetRE = `^RESET (?P<reason>.*)$`
)
func ParseMessage(message string) (*Message, error) {
// BUG(Josh) Only compile regexps once
init := regexp.MustCompile(initRE)
accept := regexp.MustCompile(acceptRE)
deny := regexp.MustCompile(denyRE)
dhpub := regexp.MustCompile(dhpubRE)
tunnip := regexp.MustCompile(tunnipRE)
conninfo := regexp.MustCompile(conninfoRE)
reset := regexp.MustCompile(resetRE)
data := make(map[string]string)
switch {
case init.MatchString(message):
return &Message{Type: Init, Data: data}, nil
case accept.MatchString(message):
return &Message{Type: Accept, Data: data}, nil
case deny.MatchString(message):
data["reason"] = deny.ReplaceAllString(message, "${reason}")
return &Message{Type: Deny, Data: data}, nil
case reset.MatchString(message):
data["reason"] = reset.ReplaceAllString(message, "${reason}")
return &Message{Type: Reset, Data: data}, nil
case dhpub.MatchString(message):
data["x"] = dhpub.ReplaceAllString(message, "${x}")
data["y"] = dhpub.ReplaceAllString(message, "${y}")
data["i"] = dhpub.ReplaceAllString(message, "${i}")
return &Message{Type: Dhpub, Data: data}, nil
case tunnip.MatchString(message):
data["ip"] = tunnip.ReplaceAllString(message, "${ip}")
return &Message{Type: Tunnip, Data: data}, nil
case conninfo.MatchString(message):
data["ip"] = conninfo.ReplaceAllString(message, "${ip}")
data["port"] = conninfo.ReplaceAllString(message, "${port}")
return &Message{Type: Conninfo, Data: data}, nil
default:
return nil, messageError{fmt.Sprintf("Failed to parse message: %s", message)}
}
}
func (m Message) String() string {
switch m.Type {
case Init:
return "INIT"
case Deny:
return fmt.Sprintf("DENY %s", m.Data["reason"])
case Accept:
return "ACCEPT"
case Dhpub:
return fmt.Sprintf("DHPUB %s %s %s", m.Data["i"], m.Data["x"], m.Data["y"])
case Tunnip:
return fmt.Sprintf("TUNNIP %s", m.Data["ip"])
case Conninfo:
return fmt.Sprintf("CONNINFO %s %s", m.Data["ip"], m.Data["port"])
case Reset:
return fmt.Sprintf("RESET %s", m.Data["reason"])
}
return ""
}
func (m Message) DhParams() (*big.Int, *big.Int, int, error) {
if m.Type == Dhpub {
xBytes, err := base64.StdEncoding.DecodeString(m.Data["x"])
if err != nil {
return nil, nil, 0, err
}
yBytes, err := base64.StdEncoding.DecodeString(m.Data["y"])
if err != nil {
return nil, nil, 0, err
}
x := &big.Int{}
y := &big.Int{}
x.SetBytes(xBytes)
y.SetBytes(yBytes)
i, _ := strconv.Atoi(m.Data["i"])
return x, y, i, nil
}
return nil, nil, 0, nil
}
func (m Message) IPInfo() (net.IP,int) {
if m.Type == Tunnip {
return net.ParseIP(m.Data["ip"]),0
}
if m.Type == Conninfo {
port,_ := strconv.Atoi(m.Data["port"])
return net.ParseIP(m.Data["ip"]),port
}
return nil,0
}