master
Evan Chen 2023-08-18 10:01:35 +08:00
parent d51ccc456b
commit 8e07df263d
1 changed files with 107 additions and 2 deletions

109
main.go
View File

@ -1,8 +1,10 @@
package main package main
import ( import (
"bufio"
"bytes" "bytes"
_ "embed" _ "embed"
"encoding/json"
"fmt" "fmt"
"log" "log"
"net" "net"
@ -10,6 +12,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"strings" "strings"
"sync"
"syscall" "syscall"
"text/template" "text/template"
"time" "time"
@ -21,6 +24,7 @@ var tmpl string
var report = template.Must(template.New("").Parse(tmpl)) var report = template.Must(template.New("").Parse(tmpl))
var write_report chan bool var write_report chan bool
var interval = time.Second * 2 var interval = time.Second * 2
var wr sync.Mutex
type Network struct { type Network struct {
Name string Name string
@ -67,13 +71,17 @@ func main() {
perr = true perr = true
log.Println(err) log.Println(err)
} }
wr.Lock()
fmt.Fprintf(file, `{"x": "%s", "y": "%s", "name": "%s"},`, now, "failed", "Public") fmt.Fprintf(file, `{"x": "%s", "y": "%s", "name": "%s"},`, now, "failed", "Public")
fmt.Fprintf(file, "\n") fmt.Fprintf(file, "\n")
wr.Unlock()
} else { } else {
perr = false perr = false
if print { if print {
wr.Lock()
fmt.Fprintf(file, `{"x": "%s", "y": "%s", "name": "%s"},`, now, "ok", "Public") fmt.Fprintf(file, `{"x": "%s", "y": "%s", "name": "%s"},`, now, "ok", "Public")
fmt.Fprintf(file, "\n") fmt.Fprintf(file, "\n")
wr.Unlock()
} }
} }
}(reducer == 0) }(reducer == 0)
@ -87,14 +95,18 @@ func main() {
derr = true derr = true
log.Println(err) log.Println(err)
} }
wr.Lock()
fmt.Fprintf(file, `{"x": "%s", "y": "%s", "name": "%s"},`, now, "failed", Networks[i].Name) fmt.Fprintf(file, `{"x": "%s", "y": "%s", "name": "%s"},`, now, "failed", Networks[i].Name)
fmt.Fprintf(file, "\n") fmt.Fprintf(file, "\n")
wr.Unlock()
// fmt.Fprintf(file, "%s\t%s\t%s\n", now, Networks[i].Name, "failed") // fmt.Fprintf(file, "%s\t%s\t%s\n", now, Networks[i].Name, "failed")
} else { } else {
derr = false derr = false
if print { if print {
wr.Lock()
fmt.Fprintf(file, `{"x": "%s", "y": "%s", "name": "%s"},`, now, "ok", Networks[i].Name) fmt.Fprintf(file, `{"x": "%s", "y": "%s", "name": "%s"},`, now, "ok", Networks[i].Name)
fmt.Fprintf(file, "\n") fmt.Fprintf(file, "\n")
wr.Unlock()
} }
} }
} }
@ -109,7 +121,8 @@ func main() {
go func() { go func() {
for { for {
<-time.After(time.Minute * 10) <-time.After(time.Minute * 5)
CleanLog()
WriteReport() WriteReport()
} }
}() }()
@ -118,7 +131,11 @@ func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, strings.TrimPrefix(r.URL.Path, "/")) http.ServeFile(w, r, strings.TrimPrefix(r.URL.Path, "/"))
}) })
if err := http.ListenAndServe("0.0.0.0:1080", nil); err != nil { port, ok := os.LookupEnv("PORT")
if !ok {
port = "1080"
}
if err := http.ListenAndServe("0.0.0.0:"+port, nil); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
}() }()
@ -127,6 +144,8 @@ func main() {
signal.Notify(wait, syscall.SIGINT, syscall.SIGTERM) signal.Notify(wait, syscall.SIGINT, syscall.SIGTERM)
<-wait <-wait
CleanLog()
WriteReport() WriteReport()
} }
@ -147,7 +166,9 @@ func WriteReport() {
} }
defer f.Close() defer f.Close()
wr.Lock()
data, err := os.ReadFile("network.log") data, err := os.ReadFile("network.log")
wr.Unlock()
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return
@ -162,6 +183,90 @@ func WriteReport() {
} }
type point struct {
Time string `json:"x"`
Stat string `json:"y"`
Name string `json:"name"`
}
func (p point) GetTime() time.Time {
t, _ := time.Parse("2006-01-02 15:04:05", p.Time)
return t
}
func CleanLog() error {
file, err := os.OpenFile("network.log", os.O_RDONLY|os.O_CREATE, os.ModePerm)
if err != nil {
log.Println(err)
return err
}
last := map[string]*point{}
end := map[string]*point{}
queue := []*point{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
pt := &point{}
data := scanner.Bytes()
err = json.Unmarshal(data[:len(data)-1], pt)
if err != nil {
log.Println(err)
file.Close()
return err
}
end[pt.Name] = pt
last_pt, ok := last[pt.Name]
if !ok {
last[pt.Name] = pt
queue = append(queue, pt)
} else {
if last_pt.Stat != pt.Stat {
last[pt.Name] = pt
queue = append(queue, pt)
}
}
}
if err := scanner.Err(); err != nil {
log.Println(err)
return err
}
file.Close()
for k, v := range end {
if pt, ok := last[k]; ok {
if pt.Time != v.Time {
queue = append(queue, v)
}
}
}
wr.Lock()
defer wr.Unlock()
file, err = os.OpenFile("network.log", os.O_WRONLY|os.O_TRUNC, os.ModePerm)
if err != nil {
log.Println(err)
return err
}
defer file.Close()
for i := range queue {
b, err := json.Marshal(queue[i])
if err != nil {
log.Println(err)
return err
}
file.Write(b)
file.Write([]byte{',', '\n'})
}
return nil
}
// Get preferred outbound ip of this machine // Get preferred outbound ip of this machine
func GetOutboundIP(addr string) (net.IP, error) { func GetOutboundIP(addr string) (net.IP, error) {