-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopher.go
222 lines (191 loc) · 3.78 KB
/
gopher.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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
)
const selectorcap = 4096
const timeout = 5 * time.Second
var config struct {
port int
name string
dir string
http string
}
func topath(base, sel string) string {
sel = filepath.FromSlash(sel)
return filepath.Join(base, sel)
}
func contains(sel string) bool {
return !strings.Contains(filepath.Clean(sel), "..")
}
func newselector(line string) *gopherline {
ret := gopherline{'i', "", "/", config.name, config.port}
fields := strings.Split(line, "\t")
if len(fields) > 0 && len(fields[0]) > 0 {
ret.Ftype, ret.Text = rune(fields[0][0]), fields[0][1:]
}
if len(fields) > 1 {
ret.Path = fields[1]
}
if len(fields) > 2 {
ret.Host = fields[2]
}
if len(fields) > 3 {
ret.Port, _ = strconv.Atoi(fields[3])
}
return &ret
}
func readdirfile(p string, filename string) gopherdir {
l := gopherdir{}
data, err := ioutil.ReadFile(path.Join(p, filename))
if err != nil {
return l
}
lines := strings.Split(strings.Replace(string(data), "\r", "", -1), "\n")
for _, line := range lines {
if len(line) > 0 {
l = append(l, newselector(line))
}
}
return l
}
func getheader(p string) gopherdir {
return readdirfile(p, ".head")
}
func withtemplate(p string, dir gopherdir) gopherdir {
l := readdirfile(p, ".dir")
out := gopherdir{}
for _, line := range l {
if line.Ftype == '{' && line.Text == "DIR}" {
out = append(out, dir...)
} else {
out = append(out, line)
}
}
if len(out) == 0 {
return dir
} else {
return out
}
}
func listdir(p string, sel string) gopherdir {
files, err := ioutil.ReadDir(p)
if err != nil {
log.Fatal(err)
}
dir := gopherdir{}
for _, f := range files {
fname := f.Name()
if strings.HasPrefix(fname, ".") {
continue
}
ft, err := getft(filepath.Join(p, fname))
if err != nil {
continue
}
line := gopherline{
Ftype: ft,
Text: fname,
Path: filepath.ToSlash(filepath.Join("/", sel, fname)),
Host: config.name,
Port: config.port,
}
dir = append(dir, &line)
}
return dir
}
func getdir(p, sel string) gopherdir {
dir := getheader(p)
return append(dir, withtemplate(p, listdir(p, sel))...)
}
func isdir(p string) bool {
fi, err := os.Stat(p)
if err != nil {
return false
}
return fi.IsDir()
}
func handle(conn io.ReadWriteCloser) {
defer conn.Close()
r := bufio.NewReader(io.LimitReader(conn, selectorcap))
buf, toolarge, err := r.ReadLine()
if err != nil {
notfound.serialize(conn)
return
}
if toolarge {
notfound.serialize(conn)
return
}
sel := string(buf)
if !contains(sel) {
notfound.serialize(conn)
return
}
p := topath(config.dir, sel)
fi, err := os.Stat(p)
if err != nil {
notfound.serialize(conn)
return
}
if fi.IsDir() {
getdir(p, sel).serialize(conn)
} else {
f, err := os.Open(p)
if err != nil {
notfound.serialize(conn)
return
}
defer f.Close()
_, err = io.Copy(conn, f)
if err != nil {
return
}
}
}
func main() {
hn, err := os.Hostname()
if err != nil {
hn = "127.0.0.1"
}
flag.IntVar(&config.port, "p", 70, "The port to listen to")
flag.StringVar(&config.dir, "d", ".", "The gopher root dir")
flag.StringVar(&config.name, "n", hn, "The host as listed in gopher directories")
flag.StringVar(&config.http, "w", "", "HTTP server address")
flag.Parse()
config.dir, err = filepath.Abs(config.dir)
if err != nil {
log.Fatal(err)
}
l, err := net.Listen("tcp", fmt.Sprintf(":%d", config.port))
if err != nil {
log.Fatal(err)
}
defer l.Close()
if config.http != "" {
go serveHttp(config.http)
}
for {
conn, err := l.Accept()
if err != nil {
continue
}
err = conn.SetReadDeadline(time.Now().Add(timeout))
if err != nil {
conn.Close()
continue
}
go handle(conn)
}
}