Skip to content

Commit 2955cf6

Browse files
committed
Organize code a little bit
1 parent a8c8d98 commit 2955cf6

4 files changed

Lines changed: 182 additions & 167 deletions

File tree

pkg/ciscofonserver/dashboard.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package ciscofonserver
2+
3+
import (
4+
"embed"
5+
"log"
6+
"sync"
7+
"time"
8+
9+
"github.com/gofiber/contrib/websocket"
10+
"github.com/gofiber/fiber/v2"
11+
)
12+
13+
//go:embed templates
14+
var templatesFS embed.FS
15+
16+
var connections = make(map[*websocket.Conn]bool)
17+
var connectionsMu sync.Mutex
18+
19+
type logEntry struct {
20+
Time time.Time `json:"time"`
21+
Service string `json:"service"`
22+
Method string `json:"method"`
23+
Path string `json:"path"`
24+
Status string `json:"status"`
25+
IP string `json:"ip"`
26+
}
27+
28+
func (s *CiscoFonServer) registerDashboardRoutes(app *fiber.App) {
29+
app.Get("/dashboard", func(c *fiber.Ctx) error {
30+
return c.Render("templates/dashboard", fiber.Map{})
31+
})
32+
33+
app.Get("/dashboard/log-ws", websocket.New(func(c *websocket.Conn) {
34+
connectionsMu.Lock()
35+
connections[c] = true
36+
connectionsMu.Unlock()
37+
defer func() {
38+
connectionsMu.Lock()
39+
delete(connections, c)
40+
connectionsMu.Unlock()
41+
}()
42+
43+
for {
44+
if _, _, err := c.ReadMessage(); err != nil {
45+
log.Println("read:", err)
46+
break
47+
}
48+
}
49+
}))
50+
}
51+
52+
func (s *CiscoFonServer) logRequest(service, method, path string, status string, ip string) {
53+
entry := logEntry{
54+
Time: time.Now(),
55+
Service: service,
56+
Method: method,
57+
Path: path,
58+
Status: status,
59+
IP: ip,
60+
}
61+
log.Printf("[%s] %s %s %s %s", service, ip, method, path, status)
62+
connectionsMu.Lock()
63+
defer connectionsMu.Unlock()
64+
for conn := range connections {
65+
if err := conn.WriteJSON(entry); err != nil {
66+
log.Println("write:", err)
67+
}
68+
}
69+
}

pkg/ciscofonserver/http.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package ciscofonserver
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
"strings"
9+
10+
"github.com/gofiber/fiber/v2"
11+
"github.com/gofiber/template/html/v2"
12+
)
13+
14+
func (s *CiscoFonServer) startHTTPServer() {
15+
engine := html.NewFileSystem(http.FS(templatesFS), ".html")
16+
app := fiber.New(fiber.Config{
17+
Views: engine,
18+
19+
ErrorHandler: func(c *fiber.Ctx, err error) error {
20+
// Status code defaults to 500
21+
code := fiber.StatusInternalServerError
22+
23+
// Retrieve the custom status code if it's a *fiber.Error
24+
var e *fiber.Error
25+
if errors.As(err, &e) {
26+
code = e.Code
27+
}
28+
s.logRequest("HTTP", c.Method(), c.Path(), fmt.Sprintf("%d", code), c.IP())
29+
30+
c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
31+
32+
// Return from handler
33+
return c.Status(code).SendString(err.Error())
34+
},
35+
})
36+
37+
app.Use(func(c *fiber.Ctx) error {
38+
err := c.Next()
39+
if !strings.HasPrefix(c.Path(), "/dashboard") && err == nil {
40+
s.logRequest("HTTP", c.Method(), c.Path(), "200", c.IP())
41+
}
42+
return err
43+
})
44+
45+
app.All("*.xml", func(c *fiber.Ctx) error {
46+
c.Set(fiber.HeaderContentType, "text/xml")
47+
return c.Next()
48+
})
49+
50+
app.Static("/", s.config.String("http.dir"))
51+
52+
s.registerDashboardRoutes(app)
53+
54+
log.Println("Starting HTTP server on", s.config.String("http.port"))
55+
err := app.Listen(":" + s.config.String("http.port"))
56+
if err != nil {
57+
log.Fatalf("HTTP server error: %v", err)
58+
}
59+
}

pkg/ciscofonserver/server.go

Lines changed: 0 additions & 167 deletions
This file was deleted.

pkg/ciscofonserver/tftp.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package ciscofonserver
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"log"
7+
"os"
8+
"path/filepath"
9+
"time"
10+
11+
"github.com/pin/tftp/v3"
12+
)
13+
14+
// writeHandler is called when client starts file upload to server
15+
func (s *CiscoFonServer) writeHandler(filename string, wt io.WriterTo) error {
16+
return nil
17+
}
18+
19+
// readHandler is called when client starts file download from server
20+
func (s *CiscoFonServer) readHandler(filename string, rf io.ReaderFrom) error {
21+
cleanedFilename := cleanPath(filename)
22+
filepath := filepath.Join(s.config.String("tftp.dir"), cleanedFilename)
23+
file, err := os.Open(filepath)
24+
if err != nil {
25+
return err
26+
}
27+
defer file.Close()
28+
_, err = rf.ReadFrom(file)
29+
if err != nil {
30+
return err
31+
}
32+
33+
return nil
34+
}
35+
36+
func (s *CiscoFonServer) OnSuccess(stats tftp.TransferStats) {
37+
s.logRequest("TFTP", "READ", stats.Filename, "OK", stats.RemoteAddr.String())
38+
}
39+
40+
func (s *CiscoFonServer) OnFailure(stats tftp.TransferStats, err error) {
41+
s.logRequest("TFTP", "READ", stats.Filename, "Err: "+err.Error(), stats.RemoteAddr.String())
42+
}
43+
44+
func (s *CiscoFonServer) startTFTPServer() {
45+
srv := tftp.NewServer(s.readHandler, s.writeHandler)
46+
srv.SetHook(s)
47+
srv.SetTimeout(5 * time.Second)
48+
log.Printf("Starting TFTP server on port %s, serving files from %s\n", s.config.String("tftp.port"), s.config.String("tftp.dir"))
49+
err := srv.ListenAndServe(":" + s.config.String("tftp.port"))
50+
if err != nil {
51+
fmt.Fprintf(os.Stdout, "server: %v\n", err)
52+
os.Exit(1)
53+
}
54+
}

0 commit comments

Comments
 (0)