-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocrastiproxy.go
More file actions
65 lines (57 loc) · 1.15 KB
/
procrastiproxy.go
File metadata and controls
65 lines (57 loc) · 1.15 KB
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
package procrastiproxy
import (
"fmt"
"io"
"net"
"net/http"
)
type ProxyServer struct {
Addr string
server *http.Server
listener net.Listener
blocked map[string]bool
}
func NewServer(addr string) *ProxyServer {
p := &ProxyServer{
Addr: addr,
blocked: map[string]bool{},
}
p.server = &http.Server{
Addr: addr,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if p.Deny(r.Host) {
w.WriteHeader(http.StatusForbidden)
return
}
resp, err := http.Get(r.RequestURI)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, err)
}
defer resp.Body.Close()
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}),
}
return p
}
func (p *ProxyServer) Block(link string) {
p.blocked[link] = true
}
func (p *ProxyServer) Close() error {
return p.server.Close()
}
func (p *ProxyServer) Deny(link string) bool {
return p.blocked[link]
}
func (p *ProxyServer) ListenAndServe() error {
return p.server.ListenAndServe()
}
func Main() int {
err := NewServer(":0").ListenAndServe()
if err != nil {
fmt.Printf("Error starting proxy server: %v", err)
return 1
}
return 0
}