mirror of
https://github.com/amnezia-vpn/amneziawg-go.git
synced 2025-04-16 22:16:55 +02:00
awg-2 working with identity generator
This commit is contained in:
parent
f4bc11733d
commit
1e532c1e71
6 changed files with 54 additions and 22 deletions
|
@ -69,6 +69,7 @@ func (l *Lua) Parse(data []byte) ([]byte, error) {
|
|||
|
||||
result := l.state.ToBytes(-1)
|
||||
l.state.Pop(1)
|
||||
// copy(data, result)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
|
|
@ -13,11 +13,11 @@ func newLua() *Lua {
|
|||
end
|
||||
|
||||
function d_parse(data)
|
||||
local header = "10header"
|
||||
local header = "1header"
|
||||
return string.sub(data, #header+1)
|
||||
end
|
||||
*/
|
||||
Base64LuaCode: "CmZ1bmN0aW9uIGRfZ2VuKG1zZ190eXBlLCBkYXRhLCBjb3VudGVyKQoJbG9jYWwgaGVhZGVyID0gImhlYWRlciIKCXJldHVybiBjb3VudGVyIC4uIGhlYWRlciAuLiBkYXRhCmVuZAoKZnVuY3Rpb24gZF9wYXJzZShkYXRhKQoJbG9jYWwgaGVhZGVyID0gIjEwaGVhZGVyIgoJcmV0dXJuIHN0cmluZy5zdWIoZGF0YSwgI2hlYWRlcisxKQplbmQK",
|
||||
Base64LuaCode: "CmZ1bmN0aW9uIGRfZ2VuKG1zZ190eXBlLCBkYXRhLCBjb3VudGVyKQoJbG9jYWwgaGVhZGVyID0gImhlYWRlciIKCXJldHVybiBjb3VudGVyIC4uIGhlYWRlciAuLiBkYXRhCmVuZAoKZnVuY3Rpb24gZF9wYXJzZShkYXRhKQoJbG9jYWwgaGVhZGVyID0gIjFoZWFkZXIiCglyZXR1cm4gc3RyaW5nLnN1YihkYXRhLCAjaGVhZGVyKzEpCmVuZAo=",
|
||||
})
|
||||
return lua
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ func TestLua_Parse(t *testing.T) {
|
|||
t.Run("", func(t *testing.T) {
|
||||
l := newLua()
|
||||
defer l.Close()
|
||||
got, err := l.Parse([]byte("10headertest"))
|
||||
got, err := l.Parse([]byte("1headertest"))
|
||||
if err != nil {
|
||||
t.Errorf("Lua.Parse() error = %v, wantErr %v", err, nil)
|
||||
return
|
||||
|
|
|
@ -92,7 +92,7 @@ type Device struct {
|
|||
ipcMutex sync.RWMutex
|
||||
closed chan struct{}
|
||||
log *Logger
|
||||
|
||||
|
||||
awg awgType
|
||||
}
|
||||
|
||||
|
@ -833,3 +833,15 @@ func (device *Device) handlePostConfig(tempAwgType *awgType) (err error) {
|
|||
|
||||
return err
|
||||
}
|
||||
|
||||
func (device *Device) codecPacket(msgType uint32, packet []byte) ([]byte, error) {
|
||||
if device.awg.luaAdapter != nil {
|
||||
var err error
|
||||
packet, err = device.awg.luaAdapter.Generate(int64(msgType),packet)
|
||||
if err != nil {
|
||||
device.log.Errorf("%v - Failed to run codec generate: %v", device, err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return packet, nil
|
||||
}
|
||||
|
|
|
@ -107,6 +107,7 @@ func genASecurityConfigs(tb testing.TB) (cfgs, endpointCfgs [2]string) {
|
|||
"private_key", hex.EncodeToString(key1[:]),
|
||||
"listen_port", "0",
|
||||
"replace_peers", "true",
|
||||
"lua_codec", "CQkJZnVuY3Rpb24gZF9nZW4obXNnX3R5cGUsIGRhdGEsIGNvdW50ZXIpCgkJCQlyZXR1cm4gZGF0YQoJCQllbmQKCgkJCWZ1bmN0aW9uIGRfcGFyc2UoZGF0YSkKCQkJCXJldHVybiBkYXRhCgkJCWVuZAo=",
|
||||
"jc", "5",
|
||||
"jmin", "500",
|
||||
"jmax", "1000",
|
||||
|
@ -114,8 +115,8 @@ func genASecurityConfigs(tb testing.TB) (cfgs, endpointCfgs [2]string) {
|
|||
"s2", "40",
|
||||
"h1", "123456",
|
||||
"h2", "67543",
|
||||
"h4", "32345",
|
||||
"h3", "123123",
|
||||
"h4", "32345",
|
||||
"public_key", hex.EncodeToString(pub2[:]),
|
||||
"protocol_version", "1",
|
||||
"replace_allowed_ips", "true",
|
||||
|
@ -129,6 +130,7 @@ func genASecurityConfigs(tb testing.TB) (cfgs, endpointCfgs [2]string) {
|
|||
"private_key", hex.EncodeToString(key2[:]),
|
||||
"listen_port", "0",
|
||||
"replace_peers", "true",
|
||||
"lua_codec", "CQkJZnVuY3Rpb24gZF9nZW4obXNnX3R5cGUsIGRhdGEsIGNvdW50ZXIpCgkJCQlyZXR1cm4gZGF0YQoJCQllbmQKCgkJCWZ1bmN0aW9uIGRfcGFyc2UoZGF0YSkKCQkJCXJldHVybiBkYXRhCgkJCWVuZAo=",
|
||||
"jc", "5",
|
||||
"jmin", "500",
|
||||
"jmax", "1000",
|
||||
|
@ -136,8 +138,8 @@ func genASecurityConfigs(tb testing.TB) (cfgs, endpointCfgs [2]string) {
|
|||
"s2", "40",
|
||||
"h1", "123456",
|
||||
"h2", "67543",
|
||||
"h4", "32345",
|
||||
"h3", "123123",
|
||||
"h4", "32345",
|
||||
"public_key", hex.EncodeToString(pub1[:]),
|
||||
"protocol_version", "1",
|
||||
"replace_allowed_ips", "true",
|
||||
|
@ -192,6 +194,7 @@ func (pair *testPair) Send(
|
|||
var err error
|
||||
select {
|
||||
case msgRecv := <-p0.tun.Inbound:
|
||||
fmt.Printf("len(%d) msg: %x\nlen(%d) rec: %x\n", len(msg), msg, len(msgRecv), msgRecv)
|
||||
if !bytes.Equal(msg, msgRecv) {
|
||||
err = fmt.Errorf("%s did not transit correctly", ping)
|
||||
}
|
||||
|
@ -275,7 +278,7 @@ func TestTwoDevicePing(t *testing.T) {
|
|||
}
|
||||
|
||||
// Run test with -race=false to avoid the race for setting the default msgTypes 2 times
|
||||
func TestTwoDevicePingASecurity(t *testing.T) {
|
||||
func TestASecurityTwoDevicePing(t *testing.T) {
|
||||
goroutineLeakCheck(t)
|
||||
pair := genTestPair(t, true, true)
|
||||
t.Run("ping 1.0.0.1", func(t *testing.T) {
|
||||
|
|
|
@ -9,9 +9,11 @@ import (
|
|||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/amnezia-vpn/amneziawg-go/conn"
|
||||
"golang.org/x/crypto/chacha20poly1305"
|
||||
|
@ -138,8 +140,24 @@ func (device *Device) RoutineReceiveIncoming(
|
|||
|
||||
// check size of packet
|
||||
packet := bufsArrs[i][:size]
|
||||
fmt.Printf("bufsArrs size: %d\n%.100x\n", size, bufsArrs[i])
|
||||
fmt.Printf("packet before: %x\n", packet)
|
||||
if device.awg.luaAdapter != nil {
|
||||
packet, err = device.awg.luaAdapter.Parse(packet)
|
||||
ptr:= unsafe.Pointer(bufsArrs[i]) // Get pointer to the array
|
||||
slicePtr:= (*byte)(ptr) // Type conversion to the array type
|
||||
|
||||
realPacket, err := device.awg.luaAdapter.Parse(packet)
|
||||
// Copy data from newSlice to the memory pointed to by slicedPtr
|
||||
newSliceLen:= len(realPacket)
|
||||
for j:= 0; j < newSliceLen; j++ {
|
||||
*(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(slicePtr)) + uintptr(j))) = realPacket[j]
|
||||
}
|
||||
fmt.Printf("packet after: %x\n", packet)
|
||||
fmt.Printf("bufsArs after size: %d\n%.100x\n", size, bufsArrs[i])
|
||||
// diff := size - len(packet)
|
||||
// bufsArrs[i][:len(packet)] = bufsArrs[i][diff:len(packet)]
|
||||
size = len(packet)
|
||||
fmt.Println("after size: ", size)
|
||||
if err != nil {
|
||||
device.log.Verbosef("Couldn't parse message; reason: %v", err)
|
||||
continue
|
||||
|
@ -151,7 +169,7 @@ func (device *Device) RoutineReceiveIncoming(
|
|||
junkSize := msgTypeToJunkSize[assumedMsgType]
|
||||
// transport size can align with other header types;
|
||||
// making sure we have the right msgType
|
||||
msgType = binary.LittleEndian.Uint32(packet[junkSize : junkSize+4])
|
||||
msgType = binary.LittleEndian.Uint32(packet[junkSize:junkSize+4])
|
||||
if msgType == assumedMsgType {
|
||||
packet = packet[junkSize:]
|
||||
} else {
|
||||
|
@ -285,15 +303,18 @@ func (device *Device) RoutineDecryption(id int) {
|
|||
elem.counter = binary.LittleEndian.Uint64(counter)
|
||||
// copy counter to nonce
|
||||
binary.LittleEndian.PutUint64(nonce[0x4:0xc], elem.counter)
|
||||
fmt.Printf("before decrypt: %x\n", elem.packet)
|
||||
elem.packet, err = elem.keypair.receive.Open(
|
||||
content[:0],
|
||||
nonce[:],
|
||||
content,
|
||||
nil,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
elem.packet = nil
|
||||
}
|
||||
fmt.Printf("decrypt: %x\n", elem.packet)
|
||||
}
|
||||
elemsContainer.Unlock()
|
||||
}
|
||||
|
@ -551,10 +572,13 @@ func (peer *Peer) RoutineSequentialReceiver(maxBatchSize int) {
|
|||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("bufs packet: %x\n", elem.packet)
|
||||
fmt.Printf("bufs packet: %x\n", elem.buffer[len(elem.packet)+1:MessageTransportOffsetContent+len(elem.packet)])
|
||||
bufs = append(
|
||||
bufs,
|
||||
elem.buffer[:MessageTransportOffsetContent+len(elem.packet)],
|
||||
)
|
||||
fmt.Printf("bufs before send: %.100x\n", elem.buffer)
|
||||
}
|
||||
|
||||
peer.rxBytes.Add(rxBytesLen)
|
||||
|
@ -568,6 +592,7 @@ func (peer *Peer) RoutineSequentialReceiver(maxBatchSize int) {
|
|||
peer.timersDataReceived()
|
||||
}
|
||||
if len(bufs) > 0 {
|
||||
fmt.Printf("bufs: %x\n", bufs)
|
||||
_, err := device.tun.device.Write(bufs, MessageTransportOffsetContent)
|
||||
if err != nil && !device.isClosed() {
|
||||
device.log.Errorf("Failed to write packets to TUN device: %v", err)
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"sync"
|
||||
|
@ -547,18 +548,6 @@ func calculatePaddingSize(packetSize, mtu int) int {
|
|||
return paddedSize - lastUnit
|
||||
}
|
||||
|
||||
func (device *Device) codecPacket(msgType uint32, packet []byte) ([]byte, error) {
|
||||
if device.awg.luaAdapter != nil {
|
||||
var err error
|
||||
packet, err = device.awg.luaAdapter.Generate(int64(msgType),packet)
|
||||
if err != nil {
|
||||
device.log.Errorf("%v - Failed to run codec generate: %v", device, err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return packet, nil
|
||||
}
|
||||
|
||||
/* Encrypts the elements in the queue
|
||||
* and marks them for sequential consumption (by releasing the mutex)
|
||||
*
|
||||
|
@ -603,11 +592,12 @@ func (device *Device) RoutineEncryption(id int) {
|
|||
elem.packet,
|
||||
nil,
|
||||
)
|
||||
|
||||
fmt.Printf("msg: %x\n", elem.packet)
|
||||
var err error
|
||||
if elem.packet, err = device.codecPacket(DefaultMessageTransportType, elem.packet); err != nil {
|
||||
continue
|
||||
}
|
||||
fmt.Printf("msgmsg: %x\n", elem.packet)
|
||||
}
|
||||
elemsContainer.Unlock()
|
||||
}
|
||||
|
@ -662,6 +652,7 @@ func (peer *Peer) RoutineSequentialSender(maxBatchSize int) {
|
|||
peer.timersDataSent()
|
||||
}
|
||||
for _, elem := range elemsContainer.elems {
|
||||
fmt.Printf("send buffer: %.200x\n", elem.buffer)
|
||||
device.PutMessageBuffer(elem.buffer)
|
||||
device.PutOutboundElement(elem)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue