This repository has been archived by the owner on May 8, 2019. It is now read-only.
/
main.go
142 lines (103 loc) · 2.67 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
138
139
140
141
142
package main
import (
"os"
"fmt"
"log"
"errors"
"net/http"
"encoding/json"
"gopkg.in/BurntSushi/toml.v0"
"gopkg.in/Netwurx/routeros-api-go.v0"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
valid "github.com/asaskevich/govalidator"
)
type tomlConfig struct {
Routers map[string]router
}
type router struct {
Hostname string
Port int
Username string
Password string
}
func ReadConfig() tomlConfig {
var configfile = "config.toml"
_, err := os.Stat(configfile)
if err != nil {
log.Fatal("Config file is missing: ", configfile)
}
var config tomlConfig
if _, err := toml.DecodeFile(configfile, &config); err != nil {
log.Fatal(err)
}
return config
}
func send(routerName string, command string) (routeros.Reply, error) {
var err error
ros, err := routeros.New("demo.mt.lv:8728")
if err != nil {
log.Print(err)
return routeros.Reply{}, err
}
err = ros.Connect("admin", "")
if err != nil {
log.Print(err)
return routeros.Reply{}, err
}
res, err := ros.Call(command, nil)
ros.Close()
return res, err
}
func jsonError(text string) string {
message := map[string]string{"error": text}
res, _ := json.Marshal(message)
return string(res)
}
func commandHandler(c web.C, w http.ResponseWriter, r *http.Request) {
var router string = c.URLParams["router"]
var command string = c.URLParams["host"]
var param string = c.URLParams["host"]
var err error
var res routeros.Reply
switch command {
case "ping":
res, err = cmdPing(router, param)
case "tracert":
res, err = cmdTracert(router, param)
}
if err != nil {
log.Print(err)
fmt.Fprint(w, jsonError(err.Error()))
}
}
func cmdPing(router string, param string) (routeros.Reply, error) {
if (!valid.IsIP(param) && !valid.IsDNSName(param)) {
return routeros.Reply{}, errors.New("This is not a hostname or an IP address.")
}
res, err := send(router, fmt.Sprintf("/ping %s", param))
return res, err
}
func cmdTracert(router string, param string) (routeros.Reply, error) {
if (!valid.IsIP(param) && !valid.IsDNSName(param)) {
return routeros.Reply{}, errors.New("This is not a hostname or an IP address.")
}
res, err := send(router, fmt.Sprintf("/tool traceroute address=%s duration=40 count=3", param))
return res, err
}
func handleInfo(c web.C, w http.ResponseWriter, r *http.Request) {
var router string = c.URLParams["router"]
res, err := send(router, "/system/resource/print")
if err != nil {
log.Print(err)
fmt.Fprint(w, jsonError(err.Error()))
}
log.FPrint(w, res)
}
func main() {
config := ReadConfig()
goji.Get("/:router/:command/:host", commandHandler)
goji.Get("/:router/info", handleInfo)
goji.Get("/*", http.FileServer(http.Dir("web")))
goji.Serve()
}