mirror of https://github.com/jetkvm/kvm.git
Compare commits
3 Commits
d9ac573522
...
b5297e3373
Author | SHA1 | Date |
---|---|---|
|
b5297e3373 | |
|
951173ba19 | |
|
760dc494d4 |
21
network.go
21
network.go
|
@ -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)
|
||||||
|
|
63
web.go
63
web.go
|
@ -227,45 +227,50 @@ func handleDevice(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleCreatePassword(c *gin.Context) {
|
func handleCreatePassword(c *gin.Context) {
|
||||||
LoadConfig()
|
LoadConfig()
|
||||||
|
|
||||||
if config.HashedPassword != "" {
|
if config.HashedPassword != "" {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Password already set"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Password already set"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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" {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Password mode is not enabled"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var req SetPasswordRequest
|
if config.LocalAuthMode != "noPassword" {
|
||||||
if err := c.ShouldBindJSON(&req); err != nil || req.Password == "" {
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Password mode is not enabled"})
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
return
|
||||||
return
|
}
|
||||||
}
|
|
||||||
|
|
||||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
var req SetPasswordRequest
|
||||||
if err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil || req.Password == "" {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to hash password"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
config.HashedPassword = string(hashedPassword)
|
truncatedPassword := req.Password
|
||||||
config.LocalAuthToken = uuid.New().String()
|
if len(truncatedPassword) > 70 {
|
||||||
config.LocalAuthMode = "password"
|
truncatedPassword = truncatedPassword[:70]
|
||||||
if err := SaveConfig(); err != nil {
|
}
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save configuration"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the cookie
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(truncatedPassword), bcrypt.DefaultCost)
|
||||||
c.SetCookie("authToken", config.LocalAuthToken, 7*24*60*60, "/", "", false, true)
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to hash password"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusCreated, gin.H{"message": "Password set successfully"})
|
config.HashedPassword = string(hashedPassword)
|
||||||
|
config.LocalAuthToken = uuid.New().String()
|
||||||
|
config.LocalAuthMode = "password"
|
||||||
|
if err := SaveConfig(); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save configuration"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.SetCookie("authToken", config.LocalAuthToken, 7*24*60*60, "/", "", false, true)
|
||||||
|
|
||||||
|
c.JSON(http.StatusCreated, gin.H{"message": "Password set successfully"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleUpdatePassword(c *gin.Context) {
|
func handleUpdatePassword(c *gin.Context) {
|
||||||
|
|
Loading…
Reference in New Issue