Esempio n. 1
0
func init() {
	p := &big.Int{}
	p.SetBytes([]byte{
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xc9, 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34,
		0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1,
		0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, 0xcc, 0x74,
		0x02, 0x0b, 0xbe, 0xa6, 0x3b, 0x13, 0x9b, 0x22,
		0x51, 0x4a, 0x08, 0x79, 0x8e, 0x34, 0x04, 0xdd,
		0xef, 0x95, 0x19, 0xb3, 0xcd, 0x3a, 0x43, 0x1b,
		0x30, 0x2b, 0x0a, 0x6d, 0xf2, 0x5f, 0x14, 0x37,
		0x4f, 0xe1, 0x35, 0x6d, 0x6d, 0x51, 0xc2, 0x45,
		0xe4, 0x85, 0xb5, 0x76, 0x62, 0x5e, 0x7e, 0xc6,
		0xf4, 0x4c, 0x42, 0xe9, 0xa6, 0x37, 0xed, 0x6b,
		0x0b, 0xff, 0x5c, 0xb6, 0xf4, 0x06, 0xb7, 0xed,
		0xee, 0x38, 0x6b, 0xfb, 0x5a, 0x89, 0x9f, 0xa5,
		0xae, 0x9f, 0x24, 0x11, 0x7c, 0x4b, 0x1f, 0xe6,
		0x49, 0x28, 0x66, 0x51, 0xec, 0xe6, 0x53, 0x81,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	})
	g := big.NewInt(2)
	a := big.NewInt(2048)
	xa := &big.Int{}
	xa.Exp(g, a, p)
	if len(xa.Bytes()) != 0x80 {
		utils.Panic("invalid private key, public key size should be 0x80")
	}
	dh.p, dh.g, dh.a, dh.xa = p, g, a, xa
}
Esempio n. 2
0
func newHandshake() *Handshake {
	h := &Handshake{}
	h.AESEngine = rtmfp.NewAESEngine()
	if err := h.SetKey(cryptkey, cryptkey); err != nil {
		utils.Panic(fmt.Sprintf("handshake init error = '%v'", err))
	}
	h.DHEngine = rtmfp.NewDHEngine()
	counts.Count("handshake.new", 1)
	return h
}
Esempio n. 3
0
func checksum(bs []byte) uint16 {
	if len(bs)%2 != 0 {
		utils.Panic("checksum data length")
	}
	sum := uint32(0)
	cnt := len(bs) / 2
	for i := 0; i < cnt; i++ {
		sum += uint32(uint8(bs[i*2])) * 256
		sum += uint32(uint8(bs[i*2+1]))
	}
	sum = (sum >> 16) + (sum & 0xffff)
	sum = (sum >> 16) + (sum & 0xffff)
	return uint16(^sum)
}
Esempio n. 4
0
func newLogger(loggers map[string]*Logger, name string) *Logger {
	if l := loggers[name]; l != nil {
		return l
	} else {
		if len(name) == 0 {
			utils.Panic("invalid logger name")
		}
		l = &Logger{}
		l.filename = fmt.Sprintf("log/%s", name)
		l.file = nil
		l.Printf = func(format string, v ...interface{}) {}
		loggers[name] = l
		log.Printf("[logger]: add logger '%s'\n", l.filename)
		return l
	}
}
Esempio n. 5
0
func (b *buffer) init(data []byte, roff, woff int) {
	b.data = data
	if b.setoffset(roff, woff) != nil {
		utils.Panic("bad offset")
	}
}
Esempio n. 6
0
func init() {
	var ncpu, parallel, manage, heartbeat int
	var rtmfp, listen, remote, http, apps, retrans string
	var debug bool

	flag.IntVar(&ncpu, "ncpu", 1, "maximum number of CPUs, in [1, 1024]")
	flag.IntVar(&parallel, "parallel", 32, "number of parallel worker-routins per connection, in [1, 1024]")
	flag.StringVar(&rtmfp, "rtmfp", "1935", "rtmfp ports list, for example, '1935,1936,1937'")
	flag.StringVar(&listen, "listen", "", "rpc listen port")
	flag.StringVar(&remote, "remote", "", "rpc remote port")
	flag.IntVar(&manage, "manage", 500, "session management interval, in [100, 10000] milliseconds")
	flag.StringVar(&retrans, "retrans", "500,500,1000,1500,1500,2500,3000,4000,5000,7500,10000,15000", "retransmission intervals, in [100, 30000] milliseconds")
	flag.StringVar(&http, "http", "", "default http port")
	flag.StringVar(&apps, "apps", "", "application names, separated by comma")
	flag.IntVar(&heartbeat, "heartbeat", 60, "keep alive message from server, in [1, 60] seconds")
	flag.BoolVar(&debug, "debug", false, "send log to stdio")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage:\n")
		flag.PrintDefaults()
	}
	flag.Parse()

	defer func() {
		if x := recover(); x != nil {
			fmt.Fprintf(os.Stderr, "parse argument(s) failed:\n")
			fmt.Fprintf(os.Stderr, "        %s\n", x)
			os.Exit(1)
		}
	}()

	args.debug = debug

	if ncpu < 1 {
		utils.Panic(fmt.Sprintf("invalid ncpu = %d", ncpu))
	} else {
		args.ncpu = ncpu
	}

	if parallel < 1 || parallel > 1024 {
		utils.Panic(fmt.Sprintf("invalid parallel = %d", parallel))
	} else {
		args.parallel = parallel
	}

	if ports, err := parsePorts(rtmfp); err != nil {
		utils.Panic(fmt.Sprintf("invalid rtmfp = '%s', error = '%v'", rtmfp, err))
	} else if len(ports) == 0 {
		utils.Panic(fmt.Sprintf("invalid rtmfp = '%s'", rtmfp))
	} else {
		args.udp.listen = ports
	}

	if listen = trimSpace(listen); len(listen) == 0 {
		args.rpc.listen = 0
	} else if port, err := parsePort(listen); err != nil {
		utils.Panic(fmt.Sprintf("invalid listen = '%s', error = '%v'", listen, err))
	} else {
		args.rpc.listen = port
	}

	if remote = trimSpace(remote); len(remote) == 0 {
		args.rpc.remote.port = 0
	} else if ip, port, err := parseAddr(remote); err != nil {
		utils.Panic(fmt.Sprintf("invalid remote = '%s', error = '%v'", remote, err))
	} else {
		args.rpc.remote.ip, args.rpc.remote.port = ip, port
	}

	if manage < 100 || manage > 10000 {
		utils.Panic(fmt.Sprintf("invalid manage = %d", manage))
	} else {
		args.manage = manage
	}

	if heartbeat < 1 || heartbeat > 60 {
		utils.Panic(fmt.Sprintf("invalid heartbeat = %d", heartbeat))
	} else {
		args.heartbeat = heartbeat
	}

	if values, err := parseInts(retrans); err != nil {
		utils.Panic(fmt.Sprintf("invalid retrans = '%s', error = '%v'", retrans, err))
	} else if len(values) == 0 {
		utils.Panic(fmt.Sprintf("invalid retrans = '%s'", retrans))
	} else {
		for _, v := range values {
			if v < 100 || v > 30000 {
				utils.Panic(fmt.Sprintf("invalid retrans = '%s'", retrans))
			}
		}
		args.retrans = values
	}

	if http = trimSpace(http); len(http) == 0 {
		args.http = 0
	} else if port, err := parsePort(http); err != nil {
		utils.Panic(fmt.Sprintf("invalid http = '%s', error = '%v'", http, err))
	} else {
		args.http = port
	}

	if apps = trimSpace(apps); len(apps) == 0 {
		args.apps = []string{}
	} else {
		set := make(map[string]string)
		for _, s := range strings.Split(apps, ",") {
			if app := trimSpace(s); len(app) != 0 {
				set[app] = app
			}
		}
		for app, _ := range set {
			args.apps = append(args.apps, app)
		}
	}

	if loc, err := time.LoadLocation("Asia/Shanghai"); err != nil {
		log.Printf("[location]: set location failed, error = '%v'\n", err)
	} else {
		log.Printf("[location]: set location = '%v'\n", loc)
	}
	log.Printf("[argument]: %+v", args)

	runtime.GOMAXPROCS(args.ncpu)
}
Esempio n. 7
0
func init() {
	if port := args.HttpPort(); port != 0 {
		go func() {
			http.HandleFunc("/summary", func(w http.ResponseWriter, r *http.Request) {
				const divMB = uint64(1024 * 1024)
				var mem runtime.MemStats
				runtime.ReadMemStats(&mem)
				s := map[string]interface{}{
					"time": map[string]interface{}{
						"current": time.Now().Unix(),
						"boot":    utils.Starttime,
					},
					"heap": map[string]interface{}{
						"alloc":   mem.HeapAlloc / divMB,
						"sys":     mem.HeapSys / divMB,
						"idle":    mem.HeapIdle / divMB,
						"inuse":   mem.HeapInuse / divMB,
						"objects": mem.HeapObjects / divMB,
					},
					"runtime": map[string]interface{}{
						"routines": runtime.NumGoroutine(),
						"nproc":    runtime.GOMAXPROCS(0),
					},
					"build": map[string]interface{}{
						"version": utils.Version,
						"compile": utils.Compile,
					},
					"cookies": cookies.Count(),
					"session": session.Summary(),
					"streams": session.Streams(),
					"counts":  counts.Snapshot(),
				}
				if b, err := json.MarshalIndent(s, "", "    "); err != nil {
					fmt.Fprintf(w, "json: error = '%v'\n", err)
				} else {
					fmt.Fprintf(w, "%s\n", string(b))
				}
			})
			http.HandleFunc("/mapsize", func(w http.ResponseWriter, r *http.Request) {
				if b, err := json.Marshal(session.MapSize()); err != nil {
					fmt.Fprintf(w, "json: error = '%v'\n", err)
				} else {
					fmt.Fprintf(w, "%s\n", string(b))
				}
			})
			http.HandleFunc("/dumpall", func(w http.ResponseWriter, r *http.Request) {
				if b, err := json.MarshalIndent(session.DumpAll(), "", "    "); err != nil {
					fmt.Fprintf(w, "json: error = '%v'\n", err)
				} else {
					fmt.Fprintf(w, "%s\n", string(b))
				}
			})
			http.HandleFunc("/streams", func(w http.ResponseWriter, r *http.Request) {
				if b, err := json.MarshalIndent(session.DumpStreams(), "", "    "); err != nil {
					fmt.Fprintf(w, "json: error = '%v'\n", err)
				} else {
					fmt.Fprintf(w, "%s\n", string(b))
				}
			})
			if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
				utils.Panic(fmt.Sprintf("http.listen = %d, error = '%v'", port, err))
			}
		}()
	}
}