This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
web.go
127 lines (114 loc) · 3.03 KB
/
web.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
package main
import (
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
)
var baseTempName string = "proxy"
func main() {
go cleanup_service()
http.HandleFunc("/", hello)
http.HandleFunc("/p/", proxy)
http.HandleFunc("/t/", proxyWget)
bind := fmt.Sprintf("%s:%s", os.Getenv("HOST"), os.Getenv("PORT"))
fmt.Printf("listening on %s...", bind)
err := http.ListenAndServe(bind, nil)
if err != nil {
panic(err)
}
}
func cleanup_service() {
/* a cleaning job that will cleanup all directories older than 5 minutes from now */
for {
time.Sleep(time.Duration(120) * time.Second)
files, _ := ioutil.ReadDir(os.TempDir())
for _, f := range files {
if f.IsDir() && strings.HasPrefix(f.Name(), baseTempName) {
t := time.Now().Local()
if f.ModTime().Before(t.Add(time.Duration(-120) * time.Second)) {
os.RemoveAll(filepath.Join(os.TempDir(), f.Name()))
}
}
}
}
}
func hello(res http.ResponseWriter, req *http.Request) {
fmt.Fprintf(res, "hello, world from %s <br/>", runtime.Version())
files, _ := ioutil.ReadDir(os.TempDir())
count := 0
for _, f := range files {
if f.IsDir() && strings.HasPrefix(f.Name(), baseTempName) {
count++
//fmt.Fprintf(res, "%d - %s <br/>", count, f.Name())
}
}
fmt.Fprintf(res, "%d - total <br/>", count)
}
func proxy(res http.ResponseWriter, req *http.Request) {
/* simple function that will fetch single resource and tunnel the response
useful for simple txt or image files
*/
target := req.URL.Query().Get("target")
if len(target) != 0 {
client := &http.Client{}
r, err := http.NewRequest("GET", target, nil)
if err != nil {
io.WriteString(res, "1001 "+err.Error())
return
}
resp, err := client.Do(r)
if err != nil {
io.WriteString(res, "1002 "+err.Error())
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
io.WriteString(res, "1003 "+err.Error())
return
}
io.WriteString(res, string(body))
}
}
func proxyWget(res http.ResponseWriter, req *http.Request) {
target := req.URL.Query().Get("target")
if len(target) != 0 {
//make temp dir
tempPath, err := ioutil.TempDir("", baseTempName)
if err != nil {
io.WriteString(res, "1004 "+err.Error())
return
}
os.Chdir(tempPath) //do this to make sure wget will fetch contents in this dir
_, err = exec.Command("wget", "-p", target).Output()
if err != nil {
io.WriteString(res, "1005 "+err.Error())
return
}
u, _ := url.ParseRequestURI(target)
h := u.Host
fp := filepath.Join(tempPath, h)
b := "/" + rand_str(15) + "/"
http.Handle(b, http.StripPrefix(b, http.FileServer(http.Dir(fp))))
http.Redirect(res, req, b, 302)
}
}
func rand_str(str_size int) string {
//https://devpy.wordpress.com/2013/10/24/create-random-string-in-golang/
alphanum := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, str_size)
rand.Read(bytes)
for i, b := range bytes {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
return string(bytes)
}