/
main.go
118 lines (100 loc) · 3.16 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"golang.org/x/crypto/bcrypt"
"github.com/greatdanton/goScience/controller"
"github.com/greatdanton/goScience/global"
)
// Configuration struct created for reading config from file
type Configuration struct {
Port string
Password string
ScihubURL string
}
// main function
func main() {
config, err := ReadConfiguration()
if err != nil {
fmt.Println(err)
return
}
PORT := config.Port
global.PASSWORD = config.Password
global.ScihubURL = config.ScihubURL
// handling download section
http.HandleFunc("/", authMiddleware(controller.DownloadArticle))
http.HandleFunc("/login", loginMiddleware(controller.Login))
http.HandleFunc("/captcha", authMiddleware(controller.Captcha))
// serving css & public stuff
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("./public"))))
// start webserver
log.Print("Started server on http://127.0.0.1:" + PORT)
if err := http.ListenAndServe(":"+PORT, nil); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
// authMiddleware checks if user is already authenticated. If the user is
// not authenticated it sends him to /login otherwise he is able to
// access downloading part of the application
func authMiddleware(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// check if cookie with hashed password exist
cookie, err := r.Cookie("GoScience")
if err != nil { // cookie does not exist
fmt.Println(err)
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
// check if password in cookie is the same as server set password
passHash := cookie.Value
err = bcrypt.CompareHashAndPassword([]byte(passHash), []byte(global.PASSWORD))
if err != nil {
fmt.Println(err)
http.Redirect(w, r, "/login", http.StatusSeeOther)
return
}
// password is correct, serve the request
next.ServeHTTP(w, r)
})
}
// loginMiddleware checks if user is already authenticated (and redirects him/her to main download page).
func loginMiddleware(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("GoScience")
if err != nil {
next.ServeHTTP(w, r)
return
}
passHash := cookie.Value
err = bcrypt.CompareHashAndPassword([]byte(passHash), []byte(global.PASSWORD))
if err != nil {
fmt.Println(err)
next.ServeHTTP(w, r)
return
}
// password is correct, just redirect user to download page -> "/"
http.Redirect(w, r, "/", http.StatusSeeOther)
})
}
// ReadConfiguration reads from "conf.json" file and returns Configuration struct
// which is used in main func
func ReadConfiguration() (Configuration, error) {
data, err := ioutil.ReadFile("conf.json")
if err != nil {
fmt.Println("Please add conf.json file")
return Configuration{}, err
}
config := Configuration{}
if err := json.Unmarshal(data, &config); err != nil {
return Configuration{}, err
}
// check if scihub url is present in configuration
if len(config.ScihubURL) < 1 {
return Configuration{}, fmt.Errorf("ScihubURL is not present in configuration")
}
return config, nil
}