Compare commits

...

3 Commits

Author SHA1 Message Date
Srujan S b5297e3373
Merge 760dc494d4 into 951173ba19 2025-02-13 19:25:41 +01:00
Andrew 951173ba19
Restart mDNS every time the connection information changes (#155) 2025-02-13 18:10:47 +01:00
Srujan S 760dc494d4
Update web.go
made changes to truncate to 70 characters
2025-01-10 19:18:32 -06:00
2 changed files with 52 additions and 32 deletions

View File

@ -13,6 +13,8 @@ import (
"github.com/vishvananda/netlink/nl" "github.com/vishvananda/netlink/nl"
) )
var mDNSConn *mdns.Conn
var networkState struct { var networkState struct {
Up bool Up bool
IPv4 string IPv4 string
@ -91,13 +93,26 @@ func checkNetworkState() {
} }
if newState != networkState { if newState != networkState {
networkState = newState
fmt.Println("network state changed") fmt.Println("network state changed")
//restart MDNS
startMDNS()
networkState = newState
requestDisplayUpdate() requestDisplayUpdate()
} }
} }
func startMDNS() error { func startMDNS() error {
//If server was previously running, stop it
if mDNSConn != nil {
fmt.Printf("Stopping mDNS server\n")
err := mDNSConn.Close()
if err != nil {
fmt.Printf("failed to stop mDNS server: %v\n", err)
}
}
//Start a new server
fmt.Printf("Starting mDNS server on jetkvm.local\n")
addr4, err := net.ResolveUDPAddr("udp4", mdns.DefaultAddressIPv4) addr4, err := net.ResolveUDPAddr("udp4", mdns.DefaultAddressIPv4)
if err != nil { if err != nil {
return err return err
@ -118,10 +133,11 @@ func startMDNS() error {
return err return err
} }
_, err = mdns.Server(ipv4.NewPacketConn(l4), ipv6.NewPacketConn(l6), &mdns.Config{ mDNSConn, err = mdns.Server(ipv4.NewPacketConn(l4), ipv6.NewPacketConn(l6), &mdns.Config{
LocalNames: []string{"jetkvm.local"}, //TODO: make it configurable LocalNames: []string{"jetkvm.local"}, //TODO: make it configurable
}) })
if err != nil { if err != nil {
mDNSConn = nil
return err return err
} }
//defer server.Close() //defer server.Close()
@ -157,7 +173,6 @@ func init() {
} }
} }
}() }()
fmt.Println("Starting mDNS server")
err := startMDNS() err := startMDNS()
if err != nil { if err != nil {
fmt.Println("failed to run mDNS: %v", err) fmt.Println("failed to run mDNS: %v", err)

9
web.go
View File

@ -237,6 +237,7 @@ func handleCreatePassword(c *gin.Context) {
// We only allow users with noPassword mode to set a new password // We only allow users with noPassword mode to set a new password
// Users with password mode are not allowed to set a new password without providing the old password // Users with password mode are not allowed to set a new password without providing the old password
// We have a PUT endpoint for changing the password, use that instead // We have a PUT endpoint for changing the password, use that instead
if config.LocalAuthMode != "noPassword" { if config.LocalAuthMode != "noPassword" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Password mode is not enabled"}) c.JSON(http.StatusBadRequest, gin.H{"error": "Password mode is not enabled"})
return return
@ -248,7 +249,12 @@ func handleCreatePassword(c *gin.Context) {
return return
} }
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost) truncatedPassword := req.Password
if len(truncatedPassword) > 70 {
truncatedPassword = truncatedPassword[:70]
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(truncatedPassword), bcrypt.DefaultCost)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to hash password"}) c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to hash password"})
return return
@ -262,7 +268,6 @@ func handleCreatePassword(c *gin.Context) {
return return
} }
// Set the cookie
c.SetCookie("authToken", config.LocalAuthToken, 7*24*60*60, "/", "", false, true) c.SetCookie("authToken", config.LocalAuthToken, 7*24*60*60, "/", "", false, true)
c.JSON(http.StatusCreated, gin.H{"message": "Password set successfully"}) c.JSON(http.StatusCreated, gin.H{"message": "Password set successfully"})