diff --git a/net/server/internal/sturn/attribute.go b/net/server/internal/sturn/attribute.go index e3575166..76860cdd 100644 --- a/net/server/internal/sturn/attribute.go +++ b/net/server/internal/sturn/attribute.go @@ -1,6 +1,9 @@ package sturn import ( + "crypto/md5" + "crypto/hmac" + "crypto/sha1" "errors" "strings" "strconv" @@ -167,8 +170,35 @@ func writeAttribute(attribute *SturnAttribute, buf []byte, pos int) (error, int) buf[pos + 6] = 0x04 buf[pos + 7] = 0x01 return nil, 8 + } else if attribute.atrType == ATRMessageIntegrity { + buf[pos + 1], buf[pos + 0] = setAttributeType(ATRMessageIntegrity); + buf[pos + 2] = 0; + buf[pos + 3] = 0x14; + key := md5.Sum([]byte("user:databag.dweb:pass")); + + // set hash size + lengthField0 := buf[2] + lengthField1 := buf[3] + hashLength := pos + 4 + buf[2] = byte((hashLength >> 8) % 256); + buf[3] = byte(hashLength % 256); + hash := getHmac(key[:], buf[0:pos]); + buf[2] = lengthField0 + buf[3] = lengthField1 + + for i := 0; i < 20; i++ { + buf[4 + pos + i] = hash[i]; + } + + return nil, 24 } else { fmt.Println("UNKNOWN!"); } return nil, 8 } + +func getHmac(key []byte, data []byte) []byte { + mac := hmac.New(sha1.New, key) + mac.Write(data) + return mac.Sum(nil) +} diff --git a/net/server/internal/sturn/message.go b/net/server/internal/sturn/message.go index e00bc663..af4354be 100644 --- a/net/server/internal/sturn/message.go +++ b/net/server/internal/sturn/message.go @@ -145,7 +145,7 @@ func (s *Sturn) sendAllocateError(msg *SturnMessage, addr net.Addr) { }) attributes = append(attributes, SturnAttribute{ atrType: ATRRealm, - strValue: "databag", + strValue: "databag.dweb", }) response := &SturnMessage{ class: CLSError, @@ -169,14 +169,52 @@ func (s *Sturn) handleAllocateRequest(msg *SturnMessage, addr net.Addr) { return; } - port, err := s.getRelayPort(); + relayPort, err := s.getRelayPort(); if err != nil { fmt.Println(err); s.sendAllocateError(msg, addr) return } - fmt.Println("ALLOCATE REQUEST", msg, port); + address := strings.Split(addr.String(), ":") + ip := address[0]; + port, _ := strconv.Atoi(address[1]); + //port := 53046 + var attributes []SturnAttribute + attributes = append(attributes, SturnAttribute{ + atrType: ATRXorRelayedAddress, + byteValue: FAMIPv4, + intValue: int32(relayPort), +// strValue: "98.234.232.221", + strValue: "192.168.13.233", + }); + attributes = append(attributes, SturnAttribute{ + atrType: ATRLifetime, + intValue: int32(600), + }); + attributes = append(attributes, SturnAttribute{ + atrType: ATRXorMappedAddress, + byteValue: FAMIPv4, + intValue: int32(port), + strValue: ip, + }); + attributes = append(attributes, SturnAttribute{ + atrType: ATRMessageIntegrity, + }); + response := &SturnMessage{ + class: CLSResponse, + method: MEHAllocate, + transaction: msg.transaction, + attributes: attributes, + }; + + err, n := writeMessage(response, s.buf); + + if err != nil { + fmt.Printf("failed to write stun response"); + } else { + (*s.conn).WriteTo(s.buf[:n], addr); + } return }