-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
137 lines (118 loc) · 2.7 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
package main
import (
"encoding/json"
"github.com/dgrijalva/jwt-go"
"github.com/gomodule/redigo/redis"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"strings"
"time"
)
type Configuration struct {
RedisHost string
ListenAddress string
JwtKey []byte
}
type Claims struct {
Username string `json:"username"`
jwt.StandardClaims
}
var _redis redis.Conn
func redisConnect() {
var err error
_redis, err = redis.Dial("tcp", _configuration.RedisHost)
if err != nil {
panic(err)
}
}
var _configuration = Configuration{}
func readConfig() {
file, _ := os.Open(os.Args[1])
defer file.Close()
decoder := json.NewDecoder(file)
err := decoder.Decode(&_configuration)
if err != nil {
panic(err)
}
}
func initMssRsp(rsp http.ResponseWriter) {
rsp.Header().Add("Server", "mss")
}
func handleMSS(rsp http.ResponseWriter, req *http.Request) {
initMssRsp(rsp)
tknStr := req.Header.Get("X-Geegle-JWT")
claims := &Claims{}
tkn, err := jwt.ParseWithClaims(tknStr, claims, func(token *jwt.Token) (interface{}, error) {
return _configuration.JwtKey, nil
})
if err != nil {
if err == jwt.ErrSignatureInvalid {
log.Println("Signature Invalid")
rsp.WriteHeader(http.StatusUnauthorized)
return
}
log.Println("JWT Error")
log.Println(err.Error())
rsp.WriteHeader(http.StatusBadRequest)
return
}
if !tkn.Valid {
log.Println("JWT Invalid")
rsp.WriteHeader(http.StatusUnauthorized)
return
}
if !strings.HasSuffix(claims.Username, "@services.geegle.org") {
log.Println(claims.Username)
rsp.WriteHeader(http.StatusUnauthorized)
return
}
service := claims.Username[:len(claims.Username)-20]
key := service + req.URL.Path
switch req.Method {
case "GET":
value, err := redis.String(_redis.Do("GET", key))
if err != nil {
rsp.WriteHeader(http.StatusInternalServerError)
}
io.WriteString(rsp, value)
return
case "POST":
value, err := ioutil.ReadAll(req.Body)
defer req.Body.Close()
if err != nil {
rsp.WriteHeader(http.StatusInternalServerError)
return
}
_, err = _redis.Do("SET", key, value)
if err != nil {
rsp.WriteHeader(http.StatusInternalServerError)
} else {
expires := req.Header.Get("X-MSS-Expires")
if expires != "" {
_, err = _redis.Do("EXPIRE", key, expires)
if err != nil {
rsp.WriteHeader(http.StatusInternalServerError)
return
}
}
}
io.WriteString(rsp, "OK")
default:
http.Error(rsp, "Method not allowed.", http.StatusMethodNotAllowed)
}
}
func main() {
rand.Seed(time.Now().UnixNano())
readConfig()
time.Sleep(5 * time.Second)
redisConnect()
http.HandleFunc("/", handleMSS)
err := http.ListenAndServe(_configuration.ListenAddress, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}