pppoe_control/main.go
2018-12-16 20:45:31 +08:00

90 lines
1.5 KiB
Go

package main
import (
"log"
"net"
"net/http"
"os"
"time"
)
// ErrorPanic 错误检测
func ErrorPanic(err error) {
if err != nil {
log.Panic(err)
}
}
// ErrorLog 错误检测
func ErrorLog(err error) bool {
if err != nil {
// log.Println(err)
return true
}
return false
}
// Worker 维护所有系统正常的核心类
type Worker struct {
LogFile *os.File
DNET *DutiesNet
DVPN *DutiesVPN
}
func checkTunExist() bool {
ifaces, err := net.Interfaces()
if err != nil {
log.Println(err)
return false
}
for _, i := range ifaces {
if i.Name == "tun0" {
return true
}
}
return false
}
// CmdRestartHandler 接收到该命令就重启
func (worker *Worker) CmdRestartHandler(w http.ResponseWriter, r *http.Request) {
worker.DNET.SetRestart()
w.Write([]byte("ok"))
}
// CmdImOKHandler 可以ping通
func (worker *Worker) CmdImOKHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
}
func testSync(f *os.File) {
for {
time.Sleep(time.Second * 1)
f.Sync()
}
}
func main() {
log.SetFlags(log.Lshortfile | log.LstdFlags)
f, err := os.OpenFile("/root/pppoe_control.log", os.O_SYNC|os.O_APPEND|os.O_CREATE|os.O_RDWR, 0660)
ErrorPanic(err)
log.SetOutput(f)
defer f.Close()
go testSync(f)
pw := &PingWorker{}
dnet := &DutiesNet{}
dnet.Default(pw)
dvpn := &DutiesVPN{}
dvpn.Default(pw)
worker := Worker{f, dnet, dvpn}
http.HandleFunc("/pppoe/restart", worker.CmdRestartHandler)
http.HandleFunc("/pppoe/imok", worker.CmdImOKHandler)
http.ListenAndServe(":8800", nil)
}