openvpn-mgt/httpd.go

133 lines
2.9 KiB
Go
Raw Normal View History

2019-07-08 14:36:56 +00:00
package main
import (
"encoding/json"
2019-08-15 09:19:40 +00:00
"errors"
2019-07-08 14:36:56 +00:00
"fmt"
2019-07-11 10:20:08 +00:00
"io/ioutil"
2019-07-08 14:36:56 +00:00
"log"
"net/http"
2019-08-16 15:23:19 +00:00
_ "git.euclide.org/euclide/openvpn-mgt/statik"
"github.com/rakyll/statik/fs"
2019-07-08 14:36:56 +00:00
)
2019-07-11 10:20:08 +00:00
type jsonInput struct {
Action string `json:"action"`
Params jsonInputParams `json:"params"`
}
type jsonInputParams struct {
2019-08-15 09:19:40 +00:00
Server string `json:"server"`
Session int `json:"session"`
2019-08-16 22:16:57 +00:00
User string `json:"user"`
Pass string `json:"password"`
2019-07-11 10:20:08 +00:00
}
2019-07-08 14:36:56 +00:00
type HttpServer struct {
2019-08-15 09:19:40 +00:00
Port string
ovpn *OpenVpnMgt
2019-07-11 10:20:08 +00:00
}
func parseJsonQuery(r *http.Request) (*jsonInput, error) {
var in jsonInput
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &in); err !=
nil {
return nil, err
}
return &in, nil
2019-07-08 14:36:56 +00:00
}
func (h *HttpServer) handler(w http.ResponseWriter, r *http.Request) {
2019-07-15 15:14:40 +00:00
w.Header().Set("Content-type", "application/javascript")
fmt.Fprintf(w, "\n")
2019-07-08 14:36:56 +00:00
}
2019-07-11 10:20:08 +00:00
func (h *HttpServer) ajaxHandler(w http.ResponseWriter, r *http.Request) {
2019-08-15 09:19:40 +00:00
var err error
var jsonStr []byte
2019-07-11 10:20:08 +00:00
2019-07-15 15:14:40 +00:00
w.Header().Set("Content-type", "application/json")
2019-07-11 10:20:08 +00:00
// add CORS headers
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Methods", "POST")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "content-type, accept, origin, user-agent, Accept-Encoding")
// stop here if the method is OPTIONS, to allow CORS to work
if r.Method == "OPTIONS" {
return
}
// stop here if the method is OPTIONS, to allow CORS to work
if r.Method != "POST" {
http.Error(w, "post only", 405)
return
}
req, err := parseJsonQuery(r)
if err != nil {
log.Println(err)
http.Error(w, "Invalid request", 500)
return
}
2019-08-16 22:16:57 +00:00
h.ovpn.Debug(req.Action)
2019-07-11 10:20:08 +00:00
switch req.Action {
2019-08-15 09:19:40 +00:00
case "get-remotes":
jsonStr, err = json.Marshal(h.ovpn)
case "set-remote":
err = h.ovpn.SetRemote(req.Params.Server, req.Params.Session)
jsonStr = []byte("{\"status\": \"ok\"}")
2019-08-16 22:16:57 +00:00
case "auth-user-pass":
err = h.ovpn.AuthUserPass(req.Params.Session, req.Params.User, req.Params.Pass)
jsonStr = []byte("{\"status\": \"ok\"}")
2019-08-16 15:23:19 +00:00
case "get-sessions":
jsonStr, err = json.Marshal(h.ovpn)
2019-08-15 09:19:40 +00:00
case "version":
err, version := h.ovpn.Version()
if err != nil {
break
}
jsonStr, err = json.Marshal(version)
2019-07-11 10:20:08 +00:00
case "stats":
2019-08-16 15:23:19 +00:00
case "restart":
err = h.ovpn.Restart(req.Params.Session)
jsonStr = []byte("{\"status\": \"ok\"}")
2019-07-11 10:20:08 +00:00
case "kill":
2019-08-16 15:23:19 +00:00
err = h.ovpn.Kill(req.Params.Session)
jsonStr = []byte("{\"status\": \"ok\"}")
2019-07-11 10:20:08 +00:00
default:
2019-08-15 09:19:40 +00:00
err = errors.New("Invalid request")
}
if err != nil {
http.Error(w, fmt.Sprintf("Error : %s", err), 500)
return
2019-07-11 10:20:08 +00:00
}
2019-08-15 09:19:40 +00:00
fmt.Fprintf(w, "%s", jsonStr)
2019-07-11 10:20:08 +00:00
return
}
2019-08-15 09:19:40 +00:00
func NewHTTPServer(port string, s *OpenVpnMgt) {
2019-07-08 14:36:56 +00:00
h := &HttpServer{
2019-08-15 09:19:40 +00:00
Port: port,
ovpn: s,
2019-07-08 14:36:56 +00:00
}
2019-07-08 20:32:12 +00:00
2019-08-16 15:23:19 +00:00
statikFS, err := fs.New()
if err != nil {
log.Fatal(err)
}
2019-07-11 10:20:08 +00:00
http.HandleFunc("/ajax", h.ajaxHandler)
2019-08-16 15:23:19 +00:00
http.Handle("/", http.FileServer(statikFS))
2019-07-08 20:32:12 +00:00
2019-08-15 09:19:40 +00:00
log.Fatal(http.ListenAndServe(port, nil))
2019-07-08 14:36:56 +00:00
}