mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	Add an admin socket
This commit is contained in:
		
							parent
							
								
									502ab3cfaa
								
							
						
					
					
						commit
						b754d68068
					
				
					 6 changed files with 92 additions and 15 deletions
				
			
		
							
								
								
									
										61
									
								
								src/yggdrasil/admin.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								src/yggdrasil/admin.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,61 @@
 | 
			
		|||
package yggdrasil
 | 
			
		||||
 | 
			
		||||
import "net"
 | 
			
		||||
import "os"
 | 
			
		||||
import "bytes"
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
// TODO: Make all of this JSON
 | 
			
		||||
 | 
			
		||||
type admin struct {
 | 
			
		||||
	core       *Core
 | 
			
		||||
	listenaddr string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (a *admin) init(c *Core, listenaddr string) {
 | 
			
		||||
	a.core = c
 | 
			
		||||
	a.listenaddr = listenaddr
 | 
			
		||||
	go a.listen()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (a *admin) listen() {
 | 
			
		||||
	l, err := net.Listen("tcp", a.listenaddr)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		a.core.log.Printf("Admin socket failed to listen: %v", err)
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
	defer l.Close()
 | 
			
		||||
	a.core.log.Printf("Admin socket listening on %s", l.Addr().String())
 | 
			
		||||
	for {
 | 
			
		||||
		conn, err := l.Accept()
 | 
			
		||||
		if err == nil {
 | 
			
		||||
			a.handleRequest(conn)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (a *admin) handleRequest(conn net.Conn) {
 | 
			
		||||
	buf := make([]byte, 1024)
 | 
			
		||||
	_, err := conn.Read(buf)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		a.core.log.Printf("Admin socket failed to read: %v", err)
 | 
			
		||||
		conn.Close()
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	buf = bytes.Trim(buf, "\x00\r\n\t")
 | 
			
		||||
	switch string(buf) {
 | 
			
		||||
	case "ports":
 | 
			
		||||
		ports := a.core.peers.getPorts()
 | 
			
		||||
		for _, v := range ports {
 | 
			
		||||
			conn.Write([]byte(fmt.Sprintf("Found switch port %d\n", v.port)))
 | 
			
		||||
		}
 | 
			
		||||
		break
 | 
			
		||||
 | 
			
		||||
	default:
 | 
			
		||||
		conn.Write([]byte("I didn't understand that!\n"))
 | 
			
		||||
	}
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		a.core.log.Printf("Admin socket error: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	conn.Close()
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -20,6 +20,7 @@ type Core struct {
 | 
			
		|||
	router      router
 | 
			
		||||
	dht         dht
 | 
			
		||||
	tun         tunDevice
 | 
			
		||||
	admin       admin
 | 
			
		||||
	searches    searches
 | 
			
		||||
	tcp         *tcpInterface
 | 
			
		||||
	udp         *udpInterface
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -336,6 +336,14 @@ func (c *Core) DEBUG_addKCPConn(saddr string) {
 | 
			
		|||
 | 
			
		||||
////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
func (c *Core) DEBUG_setupAndStartAdminInterface(addrport string) {
 | 
			
		||||
	a := admin{}
 | 
			
		||||
	a.init(c, addrport)
 | 
			
		||||
	c.admin = a
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
func (c *Core) DEBUG_setLogger(log *log.Logger) {
 | 
			
		||||
	c.log = log
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -150,7 +150,7 @@ func (r *router) sendPacket(bs []byte) {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func (r *router) recvPacket(bs []byte, theirAddr *address, theirSubnet *subnet) {
 | 
			
		||||
  // TODO? move this into the session?
 | 
			
		||||
	// TODO? move this into the session?
 | 
			
		||||
	//fmt.Println("Recv packet")
 | 
			
		||||
	if len(bs) < 24 {
 | 
			
		||||
		util_putBytes(bs)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -44,13 +44,13 @@ type in6_aliasreq struct {
 | 
			
		|||
	ifra_addr       sockaddr_in6
 | 
			
		||||
	ifra_dstaddr    sockaddr_in6
 | 
			
		||||
	ifra_prefixmask sockaddr_in6
 | 
			
		||||
	ifra_flags			uint32
 | 
			
		||||
	ifra_flags      uint32
 | 
			
		||||
	ifra_lifetime   in6_addrlifetime
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ifreq struct {
 | 
			
		||||
	ifr_name				[16]byte
 | 
			
		||||
	ifru_mtu				uint32
 | 
			
		||||
	ifr_name [16]byte
 | 
			
		||||
	ifru_mtu uint32
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (tun *tunDevice) setupAddress(addr string) error {
 | 
			
		||||
| 
						 | 
				
			
			@ -66,7 +66,8 @@ func (tun *tunDevice) setupAddress(addr string) error {
 | 
			
		|||
	copy(ar.ifra_name[:], tun.iface.Name())
 | 
			
		||||
 | 
			
		||||
	ar.ifra_prefixmask.sin6_len = uint8(unsafe.Sizeof(ar.ifra_prefixmask))
 | 
			
		||||
	b := make([]byte, 16); binary.LittleEndian.PutUint16(b, uint16(0xFF00))
 | 
			
		||||
	b := make([]byte, 16)
 | 
			
		||||
	binary.LittleEndian.PutUint16(b, uint16(0xFF00))
 | 
			
		||||
	ar.ifra_prefixmask.sin6_addr[0] = uint16(binary.BigEndian.Uint16(b))
 | 
			
		||||
 | 
			
		||||
	ar.ifra_addr.sin6_len = uint8(unsafe.Sizeof(ar.ifra_addr))
 | 
			
		||||
| 
						 | 
				
			
			@ -74,7 +75,8 @@ func (tun *tunDevice) setupAddress(addr string) error {
 | 
			
		|||
	parts := strings.Split(strings.TrimRight(addr, "/8"), ":")
 | 
			
		||||
	for i := 0; i < 8; i++ {
 | 
			
		||||
		addr, _ := strconv.ParseUint(parts[i], 16, 16)
 | 
			
		||||
		b := make([]byte, 16); binary.LittleEndian.PutUint16(b, uint16(addr))
 | 
			
		||||
		b := make([]byte, 16)
 | 
			
		||||
		binary.LittleEndian.PutUint16(b, uint16(addr))
 | 
			
		||||
		ar.ifra_addr.sin6_addr[i] = uint16(binary.BigEndian.Uint16(b))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue