Exemple #1
0
func processRecvEvent(ev event.Event, user string) {
	serv := getProxySession(user, ev.GetHash())
	ev = event.ExtractEvent(ev)
	switch ev.GetType() {
	case event.EVENT_USER_LOGIN_TYPE:
		req := ev.(*event.UserLoginEvent)
		closeProxyUser(req.User)
	case event.EVENT_TCP_CONNECTION_TYPE:
		req := ev.(*event.SocketConnectionEvent)
		if req.Status == event.TCP_CONN_CLOSED {
			deleteProxySession(user, ev.GetHash())
		}
	case event.HTTP_REQUEST_EVENT_TYPE:
		req := ev.(*event.HTTPRequestEvent)
		err := serv.initConn(req.Method, req.GetHeader("Host"))
		if nil != err {
			log.Printf("Failed to init conn for reason:%v\n", err)
		}
		if strings.EqualFold(req.Method, "Connect") {
			res := &event.TCPChunkEvent{}
			res.SetHash(ev.GetHash())
			if nil == serv.conn {
				res.Content = []byte("HTTP/1.1 503 ServiceUnavailable\r\n\r\n")
			} else {
				res.Content = []byte("HTTP/1.1 200 OK\r\n\r\n")
				//log.Printf("Return established.\n")
			}
			offerSendEvent(res, user)
		} else {
			if nil != serv.conn {
				err := req.Write(serv.conn)
				if nil != err {
					log.Printf("Failed to write http request %v\n", err)
					deleteProxySession(user, serv.id)
					return
				}

			} else {
				res := &event.TCPChunkEvent{}
				res.SetHash(ev.GetHash())
				res.Content = []byte("HTTP/1.1 503 ServiceUnavailable\r\n\r\n")
				offerSendEvent(res, serv.user)
			}
		}
	case event.EVENT_TCP_CHUNK_TYPE:
		if nil == serv.conn {
			//log.Printf("[%d]No session conn %d", ev.GetHash())
			deleteProxySession(serv.user, serv.id)
			return
		}
		chunk := ev.(*event.TCPChunkEvent)
		//.Printf("[%d]Chunk has %d", ev.GetHash(), len(chunk.Content))
		_, err := serv.conn.Write(chunk.Content)
		if nil != err {
			log.Printf("Failed to write chunk %v\n", err)
			serv.closeSession()
			return
		}
	}
}
Exemple #2
0
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
}
Exemple #3
0
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
}
Exemple #4
0
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
}