-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
139 lines (133 loc) · 3.59 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
package main
import (
"bufio"
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"os"
"strings"
)
var (
config Config
ftconfig FiletypeConfig
)
const version = "Petri 1.1.0"
func handleConnection(c net.Conn) {
fmt.Printf("Connection Made\n")
var requestBytes []byte
var clen uint16
path := ""
clen = 0
gotclen := false
reader := bufio.NewReader(c)
for {
b, _ := reader.ReadByte()
requestBytes = append(requestBytes, b)
//read our "header" so we know when to stop
if len(requestBytes) >= 2 && !gotclen {
clb := requestBytes[:2]
buf := bytes.NewBuffer(clb)
binary.Read(buf, binary.LittleEndian, &clen)
gotclen = true
}
//if the len of our byte array = the content len, it's time to stop recieving
if (len(requestBytes) >= int(clen)+2) && gotclen {
break
}
}
path = string(requestBytes[2:])
fmt.Printf("Request recieved for path %s (total bytes read: %v / content len: %v) \n", path, len(requestBytes), clen)
if strings.Contains(path, "..") {
fmt.Println("Request contains .. - closing prematurely due to assumed security breach attempt")
c.Close()
return
}
fullpath := "data/srv" + path
info, err := os.Stat(fullpath)
if err != nil {
//we don't have what you're looking for. Sorry!
c.Write([]byte{0x22, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})
c.Close()
return
}
if info.IsDir() {
//check if we want directory listings. if not, return 0x22
if config.ListDirs {
files, err := ioutil.ReadDir(fullpath)
if err != nil {
c.Write([]byte{0x23, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})
}
output := "Directory Listing of " + path + "\n"
for _, f := range files {
output += "=> piper://" + config.Hostname + path + f.Name() + " " + f.Name() + "\n"
}
output += "> " + version + "\n"
var response []byte
response = append(response, 0x01)
conb := []byte(output)
lenb := make([]byte, 8)
binary.LittleEndian.PutUint64(lenb, uint64(len(conb)))
response = append(response, lenb...)
response = append(response, conb...)
c.Write(response)
} else {
c.Write([]byte{0x22, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})
}
} else {
//get the file! (and parse extension)
datab, err := ioutil.ReadFile(fullpath)
if err != nil {
c.Write([]byte{0x23, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})
}
var response []byte
ext := strings.Split(path, ".")[len(strings.Split(path, "."))-1]
if contains(ftconfig.TextUtf8, ext) {
response = append(response, 0x00)
} else if contains(ftconfig.TextASCII, ext) {
response = append(response, 0x02)
} else if contains(ftconfig.TextGem, ext) {
response = append(response, 0x01)
} else if contains(ftconfig.SrvRedir, ext) {
response = append(response, 0x20)
} else {
// are we sure we aren't being tricked?
if strings.HasSuffix(path, ".") {
//no.
c.Write([]byte{0x22, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})
c.Close()
return
}
response = append(response, 0x10)
}
lenb := make([]byte, 8)
binary.LittleEndian.PutUint64(lenb, uint64(len(datab)))
response = append(response, lenb...)
response = append(response, datab...)
c.Write(response)
}
c.Close()
}
func main() {
fmt.Println("Petri: The fast, simple, and flexible Piper Webserver")
datab, _ := ioutil.ReadFile("data/config.json")
json.Unmarshal(datab, &config)
datab, _ = ioutil.ReadFile("data/filetypes.json")
json.Unmarshal(datab, &ftconfig)
l, err := net.Listen("tcp4", fmt.Sprintf(":%v", (config.Port)))
if err != nil {
fmt.Println(err)
return
}
defer l.Close()
for {
c, err := l.Accept()
if err != nil {
fmt.Println(err)
return
}
go handleConnection(c)
}
}