func rosck_read_routine(user string, conn net.Conn) { defer conn.Close() readEvent := func(buf *bytes.Buffer, length uint32) (error, event.Event, uint32) { if length == 0 { if buf.Len() > 4 { err := binary.Read(buf, binary.BigEndian, &length) if nil != err { log.Printf("read len error %v\n", err) } } else { return errors.New("Not sufficient space"), nil, 0 } } if buf.Len() < int(length) { return errors.New("Not sufficient space"), nil, length } //log.Printf("Read Event length is %d\n", length) err, ev := event.DecodeEvent(buf) return err, ev, 0 } decodebuf := &(bytes.Buffer{}) tmpbuf := make([]byte, 8192) var lastEvLength uint32 lastEvLength = 0 for { n, err := conn.Read(tmpbuf) if nil != err { break } decodebuf.Write(tmpbuf[0:n]) var ev event.Event for decodebuf.Len() > 0 { err, ev, lastEvLength = readEvent(decodebuf, lastEvLength) if nil != err { //log.Printf("Failed to read decode event: %v\n", err) break } processRecvEvent(ev, user) } if decodebuf.Len() == 0 { decodebuf.Reset() } } conns := rsock_conns[user] for i, it := range conns { if conn == it { conns = append(conns[:i], conns[i+1:]...) rsock_conns[user] = conns break } } //delete(rsock_conns, user) }
func decodeEventWithTags(content []byte) (*event.EventHeaderTags, event.Event, error) { var tags event.EventHeaderTags buf := bytes.NewBuffer(content) if ok := tags.Decode(buf); !ok { return nil, nil, fmt.Errorf("Failed to decode event header tags") } err, res := event.DecodeEvent(buf) if nil != err { return nil, nil, err } res = event.ExtractEvent(res) return &tags, res, nil }
func (task *C4CumulateTask) fillContent(reader io.Reader) error { data := make([]byte, 8192) for { n, err := reader.Read(data) if nil != err { return err } task.buffer.Write(data[0:n]) for { if task.chunkLen < 0 && task.buffer.Len() >= 4 { err = binary.Read(&task.buffer, binary.BigEndian, &task.chunkLen) if nil != err { log.Printf("#################%v\n", err) break } } if task.chunkLen >= 0 && task.buffer.Len() >= int(task.chunkLen) { content := task.buffer.Next(int(task.chunkLen)) tmp := bytes.NewBuffer(content) err, evv := event.DecodeEvent(tmp) if nil == err { evv = event.ExtractEvent(evv) idx := evv.GetHash() % uint32(len(c4WriteCBChannels)) c4WriteCBChannels[idx] <- evv // c4 := getC4Session(evv.GetHash()) // if nil == c4 { // if evv.GetType() != event.EVENT_TCP_CONNECTION_TYPE { // log.Printf("[ERROR]No C4 session found for %d with type:%T\n", evv.GetHash(), evv) // } // } else { // c4.handleTunnelResponse(c4.sess, evv) // } } else { log.Printf("[ERROR]Decode event failed %v with content len:%d\n", err, task.chunkLen) } task.buffer.Truncate(task.buffer.Len()) task.chunkLen = -1 } else { break } } } return nil }
func InvokeCallback(w http.ResponseWriter, req *http.Request) { body, err := ioutil.ReadAll(req.Body) if nil != err { } user := req.Header.Get("UserToken") index := req.Header.Get("FetcherIndex") if len(index) == 0 { index = "0:1" } var idx, poolSize uint32 fmt.Sscanf(index, "%d:%d", &idx, &poolSize) send_ev_array, exist := send_evs[user] if !exist { send_evs[user] = make([]chan event.Event, poolSize) for i, _ := range send_evs[user] { send_evs[user][i] = make(chan event.Event, 1024) } } recv_ev, ok := recv_evs[user] if !ok { recv_ev = make(chan event.Event, 4096) recv_evs[user] = recv_ev go recvEventLoop(user) } //log.Printf("#####%d\n",len(body)) buf := bytes.NewBuffer(body) for { if buf.Len() == 0 { break } err, ev := event.DecodeEvent(buf) if nil != err { log.Printf("Decode event error:%v", err) break } recv_ev <- ev //recv_evs[] //getProxySession(user, ev.GetHash()).recv_evs <- ev } var send_content bytes.Buffer start := time.Now().UnixNano() tick := time.NewTicker(10 * time.Millisecond) expectedData := true for expectedData { select { case <-tick.C: if time.Now().UnixNano()-start >= 100000*1000 { expectedData = false break } continue case ev := <-send_ev_array[idx]: if nil == ev { expectedData = false break } if sessionExist(user, ev.GetHash()) { event.EncodeEvent(&send_content, ev) } if send_content.Len() >= 16*1024 { expectedData = false break } } } //strconv.Itoa() w.Header().Set("Content-Length", strconv.Itoa(send_content.Len())) w.Write(send_content.Bytes()) }
func (gae *GAEHttpConnection) requestEvent(client *http.Client, conn *SessionConnection, ev event.Event) (err error, res event.Event) { auth := gae.gaeAuth if nil == auth { auth = gae.manager.auths.Select().(*GAEAuth) } domain := auth.appid + ".appspot.com" if strings.Contains(auth.appid, ".") { domain = auth.appid } addr, _ := getLocalHostMapping(domain) scheme := MODE_HTTP if strings.EqualFold(MODE_HTTPS, gae_cfg.ConnectionMode) { scheme = MODE_HTTPS } var buf bytes.Buffer var tags event.EventHeaderTags tags.Token = auth.token tags.Encode(&buf) if ev.GetType() == event.HTTP_REQUEST_EVENT_TYPE { var compress event.CompressEvent compress.SetHash(ev.GetHash()) compress.Ev = ev compress.CompressType = gae_cfg.Compressor var encrypt event.EncryptEvent encrypt.SetHash(ev.GetHash()) encrypt.EncryptType = gae_cfg.Encrypter encrypt.Ev = &compress event.EncodeEvent(&buf, &encrypt) } else { var encrypt event.EncryptEvent encrypt.SetHash(ev.GetHash()) encrypt.EncryptType = gae_cfg.Encrypter encrypt.Ev = ev event.EncodeEvent(&buf, &encrypt) } req := &http.Request{ Method: "POST", URL: &url.URL{Scheme: scheme, Host: addr, Path: "/invoke"}, ProtoMajor: 1, ProtoMinor: 1, Host: addr, Header: make(http.Header), Body: ioutil.NopCloser(&buf), ContentLength: int64(buf.Len()), } if len(gae_cfg.UA) > 0 { req.Header.Set("User-Agent", gae_cfg.UA) } req.Close = false req.Header.Set("Connection", "keep-alive") req.Header.Set("Content-Type", "image/jpeg") var response *http.Response response, err = gaeHttpClient.Do(req) if nil != err { log.Printf("Failed to request data from GAE:%s for:%s\n", domain, err.Error()) return err, nil } else { if response.StatusCode != 200 { log.Printf("Session[%d]Invalid response:%d\n", ev.GetHash(), response.StatusCode) return fmt.Errorf("Invalid response:%d", response.StatusCode), nil } else { var buf bytes.Buffer n, err := io.Copy(&buf, response.Body) if int64(n) < response.ContentLength { return fmt.Errorf("No sufficient space in body."), nil } if nil != err { return err, nil } response.Body.Close() if !tags.Decode(&buf) { return fmt.Errorf("Failed to decode event tag"), nil } err, res = event.DecodeEvent(&buf) if nil == err { res = event.ExtractEvent(res) } return err, res } } return nil, nil }