Example #1
0
func New(listenAddr string) (*SocksServer, error) {
	server := &SocksServer{
		Signaler: signaler.NewSignaler(),
		Closer:   closer.NewCloser(),
	}
	ln, err := net.Listen("tcp", listenAddr)
	if err != nil {
		return nil, err
	}
	server.OnClose(func() {
		ln.Close()
	})
	go func() {
		for {
			conn, err := ln.Accept()
			if err != nil {
				if server.IsClosing {
					return
				}
				continue
			}
			go server.handshake(conn)
		}
	}()
	return server, nil
}
Example #2
0
func makeSession() *Session {
	session := &Session{
		Closer:             closer.NewCloser(),
		Signaler:           signaler.NewSignaler(),
		conns:              make(map[uint32]*Conn),
		closedConnIds:      list.New(),
		newTransport:       make(chan Transport, 128),
		errTransport:       make(chan Transport),
		incomingPackets:    make(chan *Packet),
		incomingPacketsMap: make(map[string]*Packet),
		incomingAcks:       make(chan *Packet),
		recvIn:             make(chan *Packet),
		Recv:               make(chan *Packet),
		outgoingPackets:    make(chan *Packet),
		sendingPacketsMap:  make(map[string]*Packet),
		outCheckTicker:     time.NewTicker(time.Millisecond * 100),
		getTransportCount:  make(chan int),
		getStatResend:      make(chan int),
	}
	recvLink := ic.Link(session.recvIn, session.Recv)
	session.OnClose(func() {
		close(recvLink)
		session.CloseSignaler()
	})
	session.setDebugEntries()
	go session.start()
	return session
}
Example #3
0
File: server.go Project: reusee/van
func NewServer(addrStr string) (*Server, error) {
	ln, err := net.Listen("tcp", addrStr)
	if err != nil {
		return nil, err
	}
	server := &Server{
		Closer:       closer.NewCloser(),
		sessions:     make(map[int64]*Session),
		newTransport: make(chan transportInfo),
		newSessionIn: make(chan *Session),
		NewSession:   make(chan *Session),
	}
	l1 := ic.Link(server.newSessionIn, server.NewSession)
	server.OnClose(func() {
		ln.Close() // close listener
		close(l1)
	})

	// accept
	go func() {
		for {
			transport, err := ln.Accept()
			if err != nil {
				if server.IsClosing { // close normally
					return
				} else {
					log.Fatal(err)
				}
			}
			go server.handleClient(transport)
		}
	}()

	// transport / session manager
	go func() {
		for {
			select {
			case <-server.WaitClosing:
				return
			case info := <-server.newTransport:
				session, ok := server.sessions[info.sessionId]
				if ok { // existing session
					session.newTransport <- info.transport
				} else { // new session
					session := server.newSession(info.sessionId, info.transport)
					server.sessions[info.sessionId] = session
					server.newSessionIn <- session
				}
			}
		}
	}()

	return server, nil
}