func NewProxyConnect(addr *string, user *string, pass *string, proxy *ProxyConfig, done chan bool) (this *ProxyConnect) {
	this = new(ProxyConnect)
	this.client = clientConnect.NewConnectImpl()
	this.servers = make(map[string]*Server)
	this.localPlayers = make(map[string]struct{})
	this.client.RegisterEvent("preconnect", func(event clientConnect.Event) {
		if len(this.servers) > 0 {
			this.serversMutex.Lock()
			this.servers = make(map[string]*Server)
			this.serversMutex.Unlock()
		}
		this.players = 0
		this.maxPlayers = 0
	})
	this.client.RegisterEvent("authenticate", func(event clientConnect.Event) {
		this.client.RequestLater(packetConnect.NewRequestAsProxy(proxy.Address, proxy.Port, *proxy.Motd, proxy.Version, *proxy.Maxplayers), func(statusCode uint8, result packetConnect.Result) {
			if result == nil {
				return
			}
			this.ResendLocalPlayers()
		})
	})
	this.client.RegisterEvent("server", func(event clientConnect.Event) {
		serverEvent := event.(*clientConnect.EventServer)
		if serverEvent.Add {
			var address string
			if serverEvent.Address == "127.0.0.1" || serverEvent.Address == "localhost" {
				address, _, _ = net.SplitHostPort(*addr)
			} else {
				address = serverEvent.Address
			}
			server := new(Server)
			server.Name = serverEvent.Server
			server.Addr = address + ":" + strconv.FormatInt(int64(serverEvent.Port), 10)
			server.SecurityKey = serverEvent.SecurityKey
			this.serversMutex.Lock()
			defer this.serversMutex.Unlock()
			this.servers[serverEvent.Server] = server
		} else {
			this.serversMutex.Lock()
			defer this.serversMutex.Unlock()
			delete(this.servers, serverEvent.Server)
		}
	})
	clientConnect.AutoAuthenticate(this.client, user, pass)
	go clientConnect.AutoConnect(this.client, addr, done)
	go func() {
		ticker := time.NewTicker(time.Second)
		for {
			select {
			case <-ticker.C:
				if !this.client.Connected() {
					break
				}
				this.QueryRemotePlayers()
			case <-done:
				ticker.Stop()
				return
			}
		}
	}()
	return
}
Example #2
0
func NewProxyConnect(addr *string, user *string, pass *string, proxy *ProxyConfig, done chan bool) (connect *ProxyConnect) {
	client := clientConnect.NewConnect()
	connect = &ProxyConnect{
		client:            client,
		servers:           make(map[string]*Server),
		serversMutex:      &sync.RWMutex{},
		localPlayers:      make(map[string]bool),
		localPlayersMutex: &sync.RWMutex{},
	}
	client.RegisterEvent("preconnect", func(event clientConnect.Event) {
		if len(connect.servers) > 0 {
			connect.serversMutex.Lock()
			connect.servers = make(map[string]*Server)
			connect.serversMutex.Unlock()
		}
		connect.players = 0
		connect.maxPlayers = 0
	})
	client.RegisterEvent("authenticate", func(event clientConnect.Event) {
		client.RequestLater(&packetConnect.RequestAsProxy{proxy.Address, proxy.Port, *proxy.Motd, proxy.Version, *proxy.Maxplayers}, func(statusCode uint8, result packetConnect.Result) {
			if result == nil {
				return
			}
			client.DispatchEvent("roled", nil)
		})
	})
	client.RegisterEvent("server", func(event clientConnect.Event) {
		serverEvent := event.(*clientConnect.EventServer)
		if serverEvent.Add {
			var address string
			if serverEvent.Address == "127.0.0.1" || serverEvent.Address == "localhost" {
				address, _, _ = net.SplitHostPort(*addr)
			} else {
				address = serverEvent.Address
			}
			connect.serversMutex.Lock()
			defer connect.serversMutex.Unlock()
			connect.servers[serverEvent.Server] = &Server{serverEvent.Server, address + ":" + strconv.FormatInt(int64(serverEvent.Port), 10), serverEvent.SecurityKey}
		} else {
			connect.serversMutex.Lock()
			defer connect.serversMutex.Unlock()
			delete(connect.servers, serverEvent.Server)
		}
	})
	client.RegisterEvent("roled", func(event clientConnect.Event) {
		connect.ResendLocalPlayers()
	})
	clientConnect.AutoAuthenticate(client, user, pass)
	go clientConnect.AutoConnect(client, addr, done)
	go func() {
		ticker := time.NewTicker(1 * time.Second)
		for {
			select {
			case <-ticker.C:
				if !client.Connected() {
					break
				}
				connect.QueryRemotePlayers()
			case <-done:
				ticker.Stop()
				return
			}
		}
	}()
	return connect
}