-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (102 loc) · 2.72 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
c "github.com/TibebeJS/ngrok-runner/config"
"github.com/TibebeJS/ngrok-runner/models"
"github.com/TibebeJS/ngrok-runner/notifier"
"github.com/robfig/cron/v3"
"github.com/spf13/viper"
)
func FetchTunnels() (models.TunnelsResponse, error) {
URL := "http://127.0.0.1:4040/api/tunnels"
resp, err := http.Get(URL)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
var cResp models.TunnelsResponse
if err := json.NewDecoder(resp.Body).Decode(&cResp); err != nil {
log.Fatal(err)
}
return cResp, nil
}
func LoadConfig() (c.Configurations, error) {
viper.SetConfigName("config")
viper.AddConfigPath("/etc/ngrok-runner/")
viper.AutomaticEnv()
viper.SetConfigType("yml")
var configuration c.Configurations
if err := viper.ReadInConfig(); err != nil {
return configuration, err
}
err := viper.Unmarshal(&configuration)
if err != nil {
return configuration, err
}
return configuration, err
}
func FetchTunnelsAndNotify(configuration c.Configurations) func() {
var telegramNotifier notifier.Notifier = ¬ifier.Telegram{
BotToken: configuration.Telegram.BotToken,
ChatId: configuration.Telegram.ChatId,
}
var webHookNotifier notifier.Notifier = ¬ifier.WebHook{
Endpoint: configuration.WebHook.Endpoint,
Auth: notifier.Auth{
Endpoint: configuration.WebHook.Auth.Endpoint,
Email: configuration.WebHook.Auth.Email,
Password: configuration.WebHook.Auth.Password,
FieldName: configuration.WebHook.FieldName,
},
}
return func() {
tunnels, err := FetchTunnels()
if err != nil {
log.Fatalln(err)
}
var tunnelsDiscovered struct {
http string
https string
}
message := "Tunnels:\n========\n"
for _, tunnel := range tunnels.Tunnels {
if tunnel.Protocol == "https" {
tunnelsDiscovered.https = tunnel.PublicURL
} else if tunnel.Protocol == "http" {
tunnelsDiscovered.http = tunnel.PublicURL
}
message += fmt.Sprintln(tunnel.Protocol, "\t-", tunnel.PublicURL)
}
if err := telegramNotifier.Notify(message); err != nil {
fmt.Println(err)
}
if err := webHookNotifier.Notify(tunnelsDiscovered.https); err != nil {
fmt.Println(err)
message := "Webhook Failure:\n========\n"
message += err.Error()
telegramNotifier.Notify(message)
}
}
}
func main() {
configuration, err := LoadConfig()
if err != nil {
log.Fatal(err)
}
script := "start_ngrok.sh"
cmd := exec.Command("nohup", "sh", script)
cmd.Start()
c := cron.New()
go FetchTunnelsAndNotify(configuration)()
c.AddFunc(configuration.General.Cron, FetchTunnelsAndNotify(configuration))
go c.Start()
sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt, os.Kill)
<-sig
}