89 lines
2.0 KiB
Go
89 lines
2.0 KiB
Go
package utils
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"log"
|
|
"time"
|
|
|
|
"net"
|
|
"unicode/utf8"
|
|
|
|
"github.com/google/gopacket"
|
|
"github.com/google/gopacket/layers"
|
|
"github.com/mdlayher/packet"
|
|
)
|
|
|
|
type SocketCall struct {
|
|
conn *packet.Conn
|
|
Iface *net.Interface
|
|
}
|
|
|
|
|
|
func splitData(message string) []string {
|
|
data := []string{}
|
|
maxPacketLength := 1449
|
|
|
|
var i, j int
|
|
|
|
for i, j = 0, maxPacketLength; j < len(message); i, j = j, j+maxPacketLength {
|
|
for !utf8.RuneStart(message[j]) {
|
|
j--
|
|
}
|
|
data = append(data, message[i:j])
|
|
}
|
|
data = append(data, message[i:])
|
|
|
|
return data
|
|
}
|
|
|
|
func GetAdapter() string {
|
|
inetInterface := ""
|
|
inetInterfaces, _ := net.Interfaces()
|
|
if inetInterfaces[0].Name == "lo" {
|
|
inetInterface = inetInterfaces[1].Name
|
|
} else {
|
|
inetInterface = inetInterfaces[0].Name
|
|
}
|
|
return inetInterface
|
|
}
|
|
|
|
func (c* SocketCall) SendPackage(buf gopacket.SerializeBuffer) error {
|
|
_, err := c.conn.WriteTo(buf.Bytes(), &packet.Addr{HardwareAddr: layers.EthernetBroadcast})
|
|
return err
|
|
}
|
|
|
|
func (c* SocketCall)SendData(message string) {
|
|
iface, _ := net.InterfaceByName(GetAdapter())
|
|
c.Iface = iface
|
|
|
|
conn, err := packet.Listen(iface, packet.Raw, int(layers.EthernetTypeIPv4), nil)
|
|
c.conn = conn
|
|
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
splitMessage := splitData(message)
|
|
|
|
for i := 0; i < len(splitMessage); i++ {
|
|
c.SendPackage(c.FileTransferPacket(splitMessage[i], uint16(i)))
|
|
time.Sleep(20 * time.Microsecond)
|
|
|
|
time.Sleep(200 * time.Microsecond)
|
|
|
|
return
|
|
}
|
|
}
|
|
|
|
func (c* SocketCall) saprusPayloadLength(pkt []byte) [2]byte{
|
|
payloadLength := make([]byte, 2)
|
|
var length [2]byte
|
|
binary.BigEndian.PutUint16(payloadLength, uint16(len(pkt)))
|
|
|
|
length[0] = payloadLength[0]
|
|
length[1] = payloadLength[1]
|
|
|
|
return length
|
|
}
|
|
|