-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.go
141 lines (125 loc) · 3.97 KB
/
common.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
//go:build !gen
// +build !gen
package onramp
import (
"net"
"net/url"
"os"
"path/filepath"
"strings"
)
//go:generate go run -tags gen ./gen.go
// GetJoinedWD returns the working directory joined with the given path.
func GetJoinedWD(dir string) (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", err
}
jwd := filepath.Join(wd, dir)
ajwd, err := filepath.Abs(jwd)
if err != nil {
return "", err
}
if _, err := os.Stat(ajwd); err != nil {
os.MkdirAll(ajwd, 0755)
}
return ajwd, nil
}
var i2pdefault, i2pkserr = GetJoinedWD("i2pkeys")
var tordefault, torkserr = GetJoinedWD("onionkeys")
var tlsdefault, tlskserr = GetJoinedWD("tlskeys")
// I2P_KEYSTORE_PATH is the place where I2P Keys will be saved.
// it defaults to the directory "i2pkeys" current working directory.
// reference it by calling I2PKeystorePath() to check for errors
var I2P_KEYSTORE_PATH = i2pdefault
// ONION_KEYSTORE_PATH is the place where Onion Keys will be saved.
// it defaults to the directory "onionkeys" current working directory.
// reference it by calling OnionKeystorePath() to check for errors
var ONION_KEYSTORE_PATH = tordefault
// TLS_KEYSTORE_PATH is the place where TLS Keys will be saved.
// it defaults to the directory "tlskeys" current working directory.
// reference it by calling TLSKeystorePath() to check for errors
var TLS_KEYSTORE_PATH = tlsdefault
// I2PKeystorePath returns the path to the I2P Keystore. If the
// path is not set, it returns the default path. If the path does
// not exist, it creates it.
func I2PKeystorePath() (string, error) {
if _, err := os.Stat(I2P_KEYSTORE_PATH); err != nil {
err := os.MkdirAll(I2P_KEYSTORE_PATH, 0755)
if err != nil {
return "", err
}
}
return I2P_KEYSTORE_PATH, nil
}
// DeleteI2PKeyStore deletes the I2P Keystore.
func DeleteI2PKeyStore() error {
return os.RemoveAll(I2P_KEYSTORE_PATH)
}
// TorKeystorePath returns the path to the Onion Keystore. If the
// path is not set, it returns the default path. If the path does
// not exist, it creates it.
func TorKeystorePath() (string, error) {
if _, err := os.Stat(ONION_KEYSTORE_PATH); err != nil {
err := os.MkdirAll(ONION_KEYSTORE_PATH, 0755)
if err != nil {
return "", err
}
}
return ONION_KEYSTORE_PATH, nil
}
// DeleteTorKeyStore deletes the Onion Keystore.
func DeleteTorKeyStore() error {
return os.RemoveAll(ONION_KEYSTORE_PATH)
}
// TLSKeystorePath returns the path to the TLS Keystore. If the
// path is not set, it returns the default path. If the path does
// not exist, it creates it.
func TLSKeystorePath() (string, error) {
if _, err := os.Stat(TLS_KEYSTORE_PATH); err != nil {
err := os.MkdirAll(TLS_KEYSTORE_PATH, 0755)
if err != nil {
return "", err
}
}
return TLS_KEYSTORE_PATH, nil
}
// DeleteTLSKeyStore deletes the TLS Keystore.
func DeleteTLSKeyStore() error {
return os.RemoveAll(TLS_KEYSTORE_PATH)
}
// Dial returns a connection for the given network and address.
// network is ignored. If the address ends in i2p, it returns an I2P connection.
// if the address ends in anything else, it returns a Tor connection.
func Dial(network, addr string) (net.Conn, error) {
url, err := url.Parse(addr)
if err != nil {
return nil, err
}
hostname := url.Hostname()
if strings.HasSuffix(hostname, ".i2p") {
return DialGarlic(network, addr)
}
return DialOnion(network, addr)
}
// Listen returns a listener for the given network and address.
// if network is i2p or garlic, it returns an I2P listener.
// if network is tor or onion, it returns an Onion listener.
// if keys ends with ".i2p", it returns an I2P listener.
func Listen(network, keys string) (net.Listener, error) {
if network == "i2p" || network == "garlic" {
return ListenGarlic(network, keys)
}
if network == "tor" || network == "onion" {
return ListenOnion(network, keys)
}
url, err := url.Parse(keys)
if err != nil {
return nil, err
}
hostname := url.Hostname()
if strings.HasSuffix(hostname, ".i2p") {
return ListenGarlic(network, keys)
}
return ListenOnion(network, keys)
}