Created
April 15, 2016 05:53
-
-
Save freekman/3ab267ee3f52fde267b6d4cd3018722a to your computer and use it in GitHub Desktop.
Teltonika Flood Client with MSG counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/rand" | |
"fmt" | |
"net" | |
"sync" | |
"time" | |
"encoding/binary" | |
"github.com/clouway/tools/listener/bincodec" | |
"bytes" | |
) | |
func main() { | |
var wg sync.WaitGroup | |
c := NewMultiClient("85.217.129.135", 8888, wg) | |
c.OpenConnections(2000) | |
} | |
type Client struct { | |
Host string | |
Port int | |
sync.WaitGroup | |
} | |
func NewMultiClient(host string, port int, wg sync.WaitGroup) *Client { | |
return &Client{host, port, wg} | |
} | |
func (c *Client) OpenConnections(count int) { | |
addr := fmt.Sprintf("%v:%v", c.Host, c.Port) | |
for i := 0; i < count; i++ { | |
c.Add(1) | |
go func() { | |
b:=make([]byte,64) | |
ts := time.Now().Unix()*1000 | |
counter:=0 | |
imei := getUIMEI() | |
for x := 0; x < 10; x++ { | |
conn, err := net.Dial("tcp", addr) | |
if err != nil { | |
fmt.Println("got error ",err.Error()) | |
return | |
} | |
//fmt.Println("[AAAAA] ", imei) | |
if _,err = conn.Write(imei); err != nil { | |
fmt.Println("got error",err.Error()) | |
conn.Close() | |
continue | |
} | |
//fmt.Println("[BBBB] ", imei) | |
if _,err = conn.Read(b); err !=nil{ | |
fmt.Println("got error",err.Error()) | |
conn.Close() | |
continue | |
} | |
//fmt.Println("[CCCCC] ", imei) | |
for i := 0; i <= 10; i++ { | |
response := getResponce(ts) | |
res:=updateCRC(response) | |
start:=time.Now() | |
if _,err = conn.Write(res); err != nil { | |
fmt.Println("got error",err.Error()) | |
conn.Close() | |
continue | |
} | |
counter++ | |
if _,err = conn.Read(b); err !=nil{ | |
fmt.Println("got error",err.Error()) | |
conn.Close() | |
continue | |
} | |
//conn.Write(res) | |
//conn.Read(b) | |
end:=time.Now() | |
fmt.Println("[Process Time] ",end.Sub(start)) | |
//fmt.Println(res) | |
if(counter == 105){ | |
ts +=time.Hour.Nanoseconds() | |
counter=0 | |
} | |
time.Sleep(1000 * time.Millisecond) | |
} | |
conn.Close() | |
} | |
c.Done() | |
}() | |
} | |
c.Wait() | |
} | |
func getUIMEI() []byte { | |
imei := []byte{0x00, 0x0f} | |
imei = append(imei, []byte(randString(15))...) | |
return imei | |
} | |
func getResponce(timeNow int64) []byte{ | |
buff := new(bytes.Buffer) | |
binary.Write(buff,binary.BigEndian,timeNow) | |
//response := []byte{ | |
// 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5f, 0x8, 0x1, | |
// 0x0, 0x0, 0x1, 0x4c,0xfe, 0xf0, 0xb0, 0xa8, | |
// 0x0, 0x0f, 0x0e, 0xa8,0x50, 0x20, 0x9a, 0x69, 0x0, 0x0, 0xdd, 0x0, 0x63, 0x14, 0x0, 0x86, 0xff, 0x11, 0x7, 0x1, 0x1, 0x2, 0x0, 0x3, 0x0, | |
// 0x15, 0x5, 0xc8, 0x0, 0x51, 0x85, 0xff, 0x86, 0x3, 0x9, 0x0, 0xa8, 0xb6, 0x0, 0x6, 0x42, 0x37, 0xa0, 0x6, 0xc7, 0x0, | |
// 0x0, 0x1, 0x44, 0x52, 0x0, 0x0, 0x0, 0x24, 0x53, 0x0, 0x0, 0x24, 0x6d, 0x54, 0x0, 0x0, 0x0, 0x2b, 0x55, 0x0, 0x0, 0xb, | |
// 0x58, 0x57, 0x1, 0x36, 0xa5, 0xaa, 0x1, 0x4e, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x18, 0x74, | |
//} | |
response:=[]byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5f, 0x8, 0x1} | |
response= append(response, buff.Bytes()...) | |
response= append(response, 0x0, 0x0f, 0x0e, 0xa8,0x50, 0x20, 0x9a, 0x69, 0x0, 0x0, 0xdd, 0x0, 0x63, 0x14, 0x0, 0x86, 0xff, 0x11, 0x7, 0x1, 0x1, 0x2, 0x0, 0x3, 0x0, | |
0x15, 0x5, 0xc8, 0x0, 0x51, 0x85, 0xff, 0x86, 0x3, 0x9, 0x0, 0xa8, 0xb6, 0x0, 0x6, 0x42, 0x37, 0xa0, 0x6, 0xc7, 0x0, | |
0x0, 0x1, 0x44, 0x52, 0x0, 0x0, 0x0, 0x24, 0x53, 0x0, 0x0, 0x24, 0x6d, 0x54, 0x0, 0x0, 0x0, 0x2b, 0x55, 0x0, 0x0, 0xb, | |
0x58, 0x57, 0x1, 0x36, 0xa5, 0xaa, 0x1, 0x4e, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x18, 0x74) | |
return response | |
} | |
func randString(n int) string { | |
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | |
var bytes = make([]byte, n) | |
rand.Read(bytes) | |
for i, b := range bytes { | |
bytes[i] = alphanum[b%byte(len(alphanum))] | |
} | |
return string(bytes) | |
} | |
func updateCRC(b []byte) []byte { | |
p :=&gprsPacket{b} | |
crc:=p.calculateCRC() | |
//fmt.Println("[CRC] ",crc) | |
bs :=new(bytes.Buffer) | |
binary.Write(bs,binary.BigEndian, crc) | |
//fmt.Println("[CRC -byte] ",bs.Bytes()) | |
res:=make([]byte,len(b)-4) | |
copy(res, b[:len(b)-4]) | |
res = append(res, bs.Bytes()...) | |
return res | |
} | |
type gprsPacket struct { | |
data []byte | |
} | |
func (p *gprsPacket) calculateCRC() int32 { | |
size := p.payloadSize() | |
payload := p.data[8 : size+8] | |
var ( | |
preset int32 = 0 & 0xFFFF | |
polynom int32 = 0xA001 & 0xFFFF | |
crc int32 = preset | |
) | |
for _, b := range payload { | |
crc = crc ^ int32(b&0xFF) | |
for i := 0; i < 8; i++ { | |
if (crc & 0x0001) != 0 { | |
crc = (crc >> 1) ^ polynom | |
} else { | |
crc = crc >> 1 | |
} | |
} | |
} | |
return crc & 0xFFFF | |
} | |
func (p *gprsPacket) payloadSize() int32 { | |
buf := bincodec.NewBuffer(p.data[4:8]) | |
return buf.ReadInt32() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment