forked from bkielbasa/garnish
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
108 lines (97 loc) · 2.73 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"net/url"
//"os"
"strings"
"inet.af/netaddr"
"github.com/bkielbasa/garnish/garnish"
"github.com/netsec-ethz/scion-apps/pkg/pan"
)
func main() {
var (
listen pan.IPPortValue
remoteAddr string
interactive bool
sequence string
preference string
)
flag.Var(&listen, "listen", "[Server] local IP:port to listen on")
flag.StringVar(&remoteAddr, "remote", "", "[Client] Remote (i.e. the server's) SCION Address (e.g. 17-ffaa:1:1,[127.0.0.1]:12345)")
flag.BoolVar(&interactive, "i", false, "Interactive path selection, prompt to choose path")
flag.StringVar(&sequence, "sequence", "", "Sequence of space separated hop predicates to specify path")
flag.StringVar(&preference, "preference", "", "Preference sorting order for paths. "+
"Comma-separated list of available sorting options: "+
strings.Join(pan.AvailablePreferencePolicies, "|"))
flag.Parse()
policy, err := pan.PolicyFromCommandline(sequence, preference, interactive)
if err != nil {
fmt.Printf(err.Error())
}
if (listen.Get().Port() > 0) == (len(remoteAddr) > 0) {
panicOnErr(fmt.Errorf("either specify -listen for server or -remote for client"))
}
if listen.Get().Port() > 0 {
runServer(listen.Get())
} else {
runGarnish(remoteAddr, policy)
}
}
func runGarnish(address string, policy pan.Policy) {
u := url.URL{Scheme: "http", Host: "localhost:8088"}
g := garnish.New(u)
fmt.Println("Starting up Garnish Server: ")
handlers := func(w http.ResponseWriter, req *http.Request) {
g.ServeHTTP(w, req, address, policy)
xcache := w.Header().Get("X-Cache")
fmt.Println("Real header:" + xcache)
}
mux := http.NewServeMux()
mux.HandleFunc("/hello", handlers)
log.Fatal(http.ListenAndServe(":8088", mux))
fmt.Println(address)
}
//change into scion
func runServer(listen netaddr.IPPort) {
conn, err := pan.ListenUDP(context.Background(), listen, nil)
if err != nil {
fmt.Printf("listen error")
}
//defer conn.Close()
fmt.Print("Starting up origin server: ")
fmt.Println(conn.LocalAddr())
for true {
buffer := make([]byte, 1024*16*1024)
n, from, err := conn.ReadFrom(buffer)
if err != nil {
fmt.Printf("read error")
}
const msg = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SCION demo</title>
</head>
<body>
<h1>Welcome to Garnish</h1>
<p>Using SCION architecture</p>
<img src="https://www.cylab.cmu.edu/_files/images/research/scion/scion-banner.png" alt="SCION banner">
</body>
</html>`
//msg := fmt.Sprintf("Aaaaa, i love it")
n, err = conn.WriteTo([]byte(msg), from)
if err != nil {
fmt.Printf("write error")
}
fmt.Printf("Wrote %d bytes.\n", n)
}
}
func panicOnErr(err error) {
if err != nil {
panic(err)
}
}