handling allocate request

This commit is contained in:
Roland Osborne 2023-04-09 11:32:18 -07:00
parent 3115367bc1
commit ef7c5f9d9b
2 changed files with 71 additions and 3 deletions

View File

@ -1,6 +1,9 @@
package sturn package sturn
import ( import (
"crypto/md5"
"crypto/hmac"
"crypto/sha1"
"errors" "errors"
"strings" "strings"
"strconv" "strconv"
@ -167,8 +170,35 @@ func writeAttribute(attribute *SturnAttribute, buf []byte, pos int) (error, int)
buf[pos + 6] = 0x04 buf[pos + 6] = 0x04
buf[pos + 7] = 0x01 buf[pos + 7] = 0x01
return nil, 8 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 { } else {
fmt.Println("UNKNOWN!"); fmt.Println("UNKNOWN!");
} }
return nil, 8 return nil, 8
} }
func getHmac(key []byte, data []byte) []byte {
mac := hmac.New(sha1.New, key)
mac.Write(data)
return mac.Sum(nil)
}

View File

@ -145,7 +145,7 @@ func (s *Sturn) sendAllocateError(msg *SturnMessage, addr net.Addr) {
}) })
attributes = append(attributes, SturnAttribute{ attributes = append(attributes, SturnAttribute{
atrType: ATRRealm, atrType: ATRRealm,
strValue: "databag", strValue: "databag.dweb",
}) })
response := &SturnMessage{ response := &SturnMessage{
class: CLSError, class: CLSError,
@ -169,14 +169,52 @@ func (s *Sturn) handleAllocateRequest(msg *SturnMessage, addr net.Addr) {
return; return;
} }
port, err := s.getRelayPort(); relayPort, err := s.getRelayPort();
if err != nil { if err != nil {
fmt.Println(err); fmt.Println(err);
s.sendAllocateError(msg, addr) s.sendAllocateError(msg, addr)
return 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 return
} }