-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
252 lines (223 loc) · 5.32 KB
/
main.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package main
import (
"bufio"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
)
type Message struct {
Status string
StatusCode int
Header Header
Exit int
}
type Header map[string][]string
func (h Header) Add(key, value string) {
h[key] = append(h[key], value)
}
func (h Header) Get(key string) string {
if value, ok := h[key]; ok {
if len(value) > 0 {
return value[0]
}
}
return ""
}
func (m *Message) String() string {
s := []string{m.Status}
for k, values := range m.Header {
for _, v := range values {
s = append(s, k+": "+v)
}
}
return strings.Join(s, "\n") + "\n\n"
}
func ReadInConfig() {
if os.Getenv("APT_TRANSPORT_I2PHTTP_CONF") == "" {
os.Setenv("APT_TRANSPORT_I2PHTTP_CONF", "/etc/apt-transport-i2phttp/i2p.conf")
} else {
os.Setenv("APT_TRANSPORT_I2PHTTP_CONF", os.Getenv("APT_TRANSPORT_I2PHTTP_CONF"))
}
if _, err := os.Stat(os.Getenv("APT_TRANSPORT_I2PHTTP_CONF")); os.IsNotExist(err) {
os.Setenv("I2P_HTTP_PROXY", "http://127.0.0.1:4444")
os.Setenv("HTTP_PROXY", "http://127.0.0.1:4444")
os.Setenv("http_proxy", "http://127.0.0.1:4444")
} else if err != nil {
log.Fatal(err)
} else {
bytes, err := ioutil.ReadFile(os.Getenv("APT_TRANSPORT_I2PHTTP_CONF"))
if err != nil {
log.Fatal(err)
}
host := "127.0.0.1"
port := "4444"
for _, val := range strings.Split(string(bytes), "\n") {
v := strings.Split(val, "=")
if len(v) == 2 {
if v[0] == "host" {
host = v[0]
}
if v[0] == "port" {
port = v[0]
}
}
}
os.Setenv("I2P_HTTP_PROXY", "http://"+host+":"+port)
os.Setenv("HTTP_PROXY", "http://"+host+":"+port)
os.Setenv("http_proxy", "http://"+host+":"+port)
}
if os.Getenv("I2P_HTTP_PROXY") == "" {
os.Setenv("I2P_HTTP_PROXY", "http://127.0.0.1:4444")
os.Setenv("HTTP_PROXY", "http://127.0.0.1:4444")
os.Setenv("http_proxy", "http://127.0.0.1:4444")
} else {
os.Setenv("HTTP_PROXY", os.Getenv("I2P_HTTP_PROXY"))
os.Setenv("http_proxy", os.Getenv("I2P_HTTP_PROXY"))
}
}
func main() {
ReadInConfig()
c := make(chan *Message)
go output(c)
sendCapabilities(c)
stdin := bufio.NewScanner(os.Stdin)
for stdin.Scan() {
line := stdin.Text()
if line == "" {
continue
}
s := strings.SplitN(line, " ", 2)
code, err := strconv.Atoi(s[0])
if err != nil {
fmt.Println("Malformed message!")
os.Exit(100)
}
request := &Message{
Status: line,
StatusCode: code,
Header: Header{},
}
for stdin.Scan() {
line := stdin.Text()
if line == "" {
process(c, request)
break
}
s := strings.SplitN(line, ": ", 2)
request.Header.Add(s[0], s[1])
}
}
}
func output(c <-chan *Message) {
for {
m := <-c
os.Stdout.Write([]byte(m.String()))
if m.Exit != 0 {
os.Exit(m.Exit)
}
}
}
func sendCapabilities(c chan<- *Message) {
caps := &Message{
Status: "100 Capabilities",
Header: Header{},
}
caps.Header.Add("Version", "1.2")
caps.Header.Add("Pipeline", "true")
caps.Header.Add("Send-Config", "true")
c <- caps
}
func process(c chan<- *Message, m *Message) {
switch m.StatusCode {
case 600:
go fetch(c, m)
case 601:
// TODO: parse config?
default:
fail := &Message{
Status: "401 General Failure",
Header: Header{},
Exit: 100,
}
fail.Header.Add("Message", "Status code not implemented")
c <- fail
}
}
func fetch(c chan<- *Message, m *Message) {
uri := m.Header.Get("URI")
filename := m.Header.Get("Filename")
// TODO: If-Modified-Since
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
c <- &Message{
Status: "400 URI Failure",
Header: Header{
"Message": []string{"Could not open file: " + err.Error()},
"URI": []string{uri},
},
}
return
}
defer file.Close()
// TODO: Fix bug with appending to existing files
// TODO: implement range requests if file already exists
realURI := strings.Replace(uri, "i2p://", "http://", 1)
resp, err := http.Get(realURI)
if err != nil {
c <- &Message{
Status: "400 URI Failure",
Header: Header{
"Message": []string{"Could not fetch URI: " + err.Error()},
"URI": []string{uri},
},
}
return
}
defer resp.Body.Close()
started := &Message{
Status: "200 URI Start",
Header: Header{
"URI": []string{uri},
},
}
// TODO: add Last-Modified header
c <- started
md5Hash := md5.New()
sha1Hash := sha1.New()
sha256Hash := sha256.New()
sha512Hash := sha512.New()
if _, err = io.Copy(io.MultiWriter(file, md5Hash, sha1Hash, sha256Hash, sha512Hash), resp.Body); err != nil {
c <- &Message{
Status: "400 URI Failure",
Header: Header{
"Message": []string{"Could not write file: " + err.Error()},
"URI": []string{uri},
},
}
return
}
success := &Message{
Status: "201 URI Done",
Header: Header{},
}
success.Header.Add("URI", uri)
success.Header.Add("Filename", filename)
// TODO Size, Last-Modified
md5Hex := hex.EncodeToString(md5Hash.Sum(nil)[:])
success.Header.Add("MD5-Hash", md5Hex)
success.Header.Add("MD5Sum-Hash", md5Hex)
success.Header.Add("SHA1-Hash", hex.EncodeToString(sha1Hash.Sum(nil)[:]))
success.Header.Add("SHA256-Hash", hex.EncodeToString(sha256Hash.Sum(nil)[:]))
success.Header.Add("SHA512-Hash", hex.EncodeToString(sha512Hash.Sum(nil)[:]))
c <- success
}