Exemplo n.º 1
0
func (upstream *Upstream) Validate() (valid bool) {
	var host, port string
	host, port, err := net.SplitHostPort(upstream.Server)
	if err != nil {
		server := upstream.Server + ":25565"
		if host, port, err = net.SplitHostPort(server); err != nil {
			log.Error("Invalid upstream server: " + upstream.Server)
			return false
		}
		log.Infof("no port information found in %s, assume 25565", upstream.Server)
		upstream.Server = server
	}
	p, err := strconv.ParseUint(port, 10, 16)
	if err != nil {
		log.Errorf("Invalid port %s: %s", port, err.Error())
		return false
	}
	host = strings.ToLower(host)
	if !CheckHost(host) {
		log.Error("Invalid upstream host: " + host)
		return false
	}
	upstream.Server = net.JoinHostPort(host, fmt.Sprintf("%d", p))
	upstream.Pattern = strings.ToLower(upstream.Pattern)
	if !CheckPattern(upstream.Pattern) {
		log.Error("Invalid pattern: " + upstream.Pattern)
		return false
	}
	if upstream.ErrorMsg.Text == "" {
		log.Warnf("Empty error text for %s, use default string", upstream.Server)
		upstream.ErrorMsg.Text = "Connection failed to " + upstream.Server
	}
	upstream.ChatMsg = ToChatMsg(&upstream.ErrorMsg)
	return true
}
Exemplo n.º 2
0
func RejectHandler(conn *WrapedSocket, initial_pkt *mcproto.MCHandShake, e *mcchat.ChatMsg) {
	if initial_pkt.NextState == 1 {
		conn.Infof("ping packet")
		pkt, err := mcproto.ReadPacket(conn)
		if err != nil {
			conn.Errorf("Error when reading status request: %s", err.Error())
			conn.Close()
			return
		}
		if !pkt.IsStatusRequest() {
			conn.Errorf("Invalid protocol: no status request.")
			conn.Close()
			return
		}
		conn.Debugf("status: request")
		resp := new(mcproto.MCStatusResponse)
		resp.Description = e
		resp.Version.Name = "minegate"
		resp.Version.Protocol = 0
		resp_pkt, err := resp.ToRawPacket()
		if err != nil {
			conn.Errorf("Unable to make packet: %s", err.Error())
			conn.Close()
			return
		}
		_, err = conn.Write(resp_pkt.ToBytes())
		if err != nil {
			log.Errorf("Unable to write response: %s", err.Error())
			conn.Close()
			return
		}
		pkt, err = mcproto.ReadPacket(conn)
		if err != nil {
			if err != io.EOF {
				log.Errorf("Unable to read packet: %s", err.Error())
			}
			conn.Close()
			return
		}
		if !pkt.IsStatusPing() {
			log.Error("Invalid protocol: no status ping.")
			conn.Close()
			return
		}
		conn.Write(pkt.ToBytes()) // Don't care now.
	} else {
		log.Info("login packet")
		kick_pkt := (*mcproto.MCKick)(e)
		raw_pkt, err := kick_pkt.ToRawPacket()
		if err != nil {
			log.Errorf("Unable to make packet: %s", err.Error())
			conn.Close()
			return
		}
		// Don't care now
		conn.Write(raw_pkt.ToBytes())
	}
	conn.Close()
	return
}
Exemplo n.º 3
0
func OnDisconnect(handle DisconnectFunc, priority int) (err error) {
	if priority < 0 || priority > 39 {
		log.Errorf("Invalid priority %d: not in range [0, 39]", priority)
		return fmt.Errorf("priority check failure: %d not in range [0, 39]", priority)
	}
	if handle == nil {
		log.Error("Attempt to register nil handler")
		return errors.New("Nil handler!")
	}
	if disconnectHandlers[priority] == nil {
		disconnectHandlers[priority] = make(disconnectHandler, 0, 16)
	}
	disconnectHandlers[priority] = append(disconnectHandlers[priority], handle)
	log.Infof("Registered disconnect handler at priority %d", priority)
	return nil
}
Exemplo n.º 4
0
func OnPreStatusResponse(handle PreStatusResponseFunc, priority int) (err error) {
	if priority < 0 || priority > 39 {
		log.Errorf("Invalid priority %d: not in range [0, 39]", priority)
		return fmt.Errorf("priority check failure: %d not in range [0, 39]", priority)
	}
	if handle == nil {
		log.Error("Attempt to register nil handler")
		return errors.New("Nil handler!")
	}
	if preStatusResponseHandlers[priority] == nil {
		preStatusResponseHandlers[priority] = make(preStatusResponseHandler, 0, 16)
	}
	preStatusResponseHandlers[priority] = append(preStatusResponseHandlers[priority], handle)
	log.Infof("Registered preStatusResponse handler at priority %d", priority)
	return nil
}
Exemplo n.º 5
0
func OnStartProxy(handle StartProxyFunc, priority int) (err error) {
	if priority < 0 || priority > 39 {
		log.Errorf("Invalid priority %d: not in range [0, 39]", priority)
		return fmt.Errorf("priority check failure: %d not in range [0, 39]", priority)
	}
	if handle == nil {
		log.Error("Attempt to register nil handler")
		return errors.New("Nil handler!")
	}
	if startProxyHandlers[priority] == nil {
		startProxyHandlers[priority] = make(startProxyHandler, 0, 16)
	}
	startProxyHandlers[priority] = append(startProxyHandlers[priority], handle)
	log.Infof("Registered startProxy handler at priority %d", priority)
	return nil
}
Exemplo n.º 6
0
func OnLoginRequest(handle LoginRequestFunc, priority int) (err error) {
	if priority < 0 || priority > 39 {
		log.Errorf("Invalid priority %d: not in range [0, 39]", priority)
		return fmt.Errorf("priority check failure: %d not in range [0, 39]", priority)
	}
	if handle == nil {
		log.Error("Attempt to register nil handler")
		return errors.New("Nil handler!")
	}
	if loginRequestHandlers[priority] == nil {
		loginRequestHandlers[priority] = make(loginRequestHandler, 0, 16)
	}
	loginRequestHandlers[priority] = append(loginRequestHandlers[priority], handle)
	log.Infof("Registered pingRequest handler at priority %d", priority)
	return nil
}