Advertisement
Guest User

blank_smsd

a guest
Jul 20th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.14 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "encoding/json"
  5.     "fmt"
  6.     "io"
  7.     "log"
  8.     "net"
  9.     "os"
  10.     "strings"
  11.  
  12.     "github.com/saintfish/chardet"
  13.     "golang.org/x/text/encoding/charmap"
  14. )
  15.  
  16. //R request
  17. type R []interface{}
  18.  
  19. func handleConnection(conn net.Conn) {
  20.     defer conn.Close()
  21.  
  22.     b := make([]byte, (1024 * 4))
  23.     var typeRequest string
  24.     for {
  25.         readLen, err := conn.Read(b)
  26.         if err == io.EOF {
  27.             return
  28.         }
  29.  
  30.         if string(b[0:1]) == "j" {
  31.             typeRequest = "json"
  32.             continue
  33.         }
  34.  
  35.         detector := chardet.NewTextDetector()
  36.         decoding, err := detector.DetectBest(b)
  37.  
  38.         if (decoding.Charset != "windows-1251" && decoding.Charset != "UTF-8") {
  39.             fmt.Println("error charset, only cp1251 or utf8")
  40.             break
  41.         }
  42.  
  43.         if err == nil {
  44.             //plain cp1251
  45.             if decoding.Charset == "windows-1251" {
  46.                 dec := charmap.Windows1251.NewDecoder()
  47.                 newBody := make([]byte, len(b)*2)
  48.                 n, _, err := dec.Transform(newBody, b, false)
  49.                 if err == nil {
  50.                     newBody = newBody[:n]
  51.                     fmt.Println(string(newBody))
  52.                 }
  53.                 break
  54.             }
  55.  
  56.             //json
  57.             if typeRequest == "json" {
  58.                 body := make([]R, 0)
  59.                 request := strings.Split(string(b[:readLen]), "\t")
  60.                 err := json.Unmarshal([]byte(request[0]), &body)
  61.                 fmt.Println(body)
  62.                 if err == nil {
  63.                     if len(body) >= 1 {
  64.                         cmd := body[0][0]
  65.                         if cmd == "send2" {
  66.                             fmt.Printf("cmd: send2, cmd_req: %v\n", cmd)
  67.                             data := body[0][1]
  68.                             for _, d := range data.([]interface{}) {
  69.                                 fmt.Println(d)
  70.                             }
  71.                         } else if cmd == "send" {
  72.                             fmt.Printf("cmd: send, cmd_req: %v\n", cmd)
  73.                             for _, data := range body[0][1].([]interface{}) {
  74.                                 for _, d := range data.([]interface{}) {
  75.                                     fmt.Println(d)
  76.                                 }
  77.                             }
  78.                         }
  79.                     }
  80.                 }
  81.                 break
  82.             } else {
  83.                 //plain
  84.                 fmt.Println(string(b[:readLen]))
  85.                 break
  86.             }
  87.         }
  88.     }
  89. }
  90.  
  91. func main() {
  92.     ln, err := net.Listen("tcp", "bill.ural.ru:3333")
  93.     if err != nil {
  94.         log.Printf("Error starting server: %s", err)
  95.         os.Exit(1)
  96.     }
  97.     for {
  98.         conn, err := ln.Accept()
  99.         if err != nil {
  100.             log.Printf("Could not accept connection: %s", err)
  101.             continue
  102.         }
  103.         go handleConnection(conn)
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement