Exemplo n.º 1
0
func ConfReload() {
	// Do not panic!
	defer func() {
		if r := recover(); r != nil {
			log.Errorf("paniced when reloading config %s, recovered.", config_file)
			log.Errorf("panic: %s", r)
		}
	}()
	log.Warn("Reloading config")
	content, err := ioutil.ReadFile(config_file)
	if err != nil {
		log.Errorf("unable to reload config %s: %s", config_file, err.Error())
		return
	}
	prev_listen := config.Listen_addr
	config_lock.Lock()
	err = yaml.Unmarshal(content, &config)
	if err != nil {
		log.Errorf("error when parsing config file %s: %s", config_file, err.Error())
		return
	}
	validateConfig()
	config_lock.Unlock()
	log.Info("config reloaded.")
	if config.Listen_addr != prev_listen {
		log.Warnf("config reload will not reopen server socket, thus no effect on listen address")
	}
	log.Infof("%d upstream server(s) found", len(config.Upstream))
}
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 loadConfig() {
	new_brust, err := minegate.GetExtraConf("conntrack.brust")
	if err != nil {
		log.Info("[conntrack] Using default brust: 5")
		brust = 5
	} else {
		nb := minegate.ToUint(new_brust)
		log.Infof("[conntrack] Using brust: %d", nb)
		brust = nb
	}
	new_limit, err := minegate.GetExtraConf("conntrack.limit")
	if err != nil {
		log.Info("[conntrack] Using default limit: 5")
		limit = 5
	} else {
		nl := minegate.ToUint(new_limit)
		if nl != 0 {
			log.Infof("[conntrack] Using limit: %d", nl)
			limit = nl
		} else {
			log.Info("[conntrack] Limit = 0, disabling conntrack.")
			limit = 0
		}
	}
	new_delta, err := minegate.GetExtraConf("conntrack.interval")
	if err != nil {
		log.Info("[conntrack] Using default delta: 60")
		delta = 60
	} else {
		nd := minegate.ToUint(new_delta)
		if nd < 15 {
			log.Warn("[conntrack] Minimal interval is 15.")
			nd = 15
		}
		log.Infof("[conntrack] Using interval %d", nd)
		delta = nd
	}
}
Exemplo n.º 4
0
func confInit() {
	content, err := ioutil.ReadFile(config_file)
	if err != nil {
		log.Fatalf("unable to load config %s: %s", config_file, err.Error())
	}
	err = yaml.Unmarshal(content, &config)
	if err != nil {
		log.Fatalf("error when parsing config file %s: %s", config_file, err.Error())
	}
	validateConfig()
	if config.Log.Target != "" && config.Log.Target != "-" {
		config.Log.Target, _ = filepath.Abs(config.Log.Target)
		log.Info("log path: " + config.Log.Target)
	}
	log.Stop()
	if config.Daemonize {
		Daemonize()
	}
	log.Start()
	if config.Log.Level != "" {
		level := log.ToLevel(config.Log.Level)
		if level == log.INVALID {
			log.Errorf("Invalid log level %s", config.Log.Level)
		} else {
			log.SetLogLevel(level)
		}
	}
	if config.Log.Target != "" && config.Log.Target != "-" {
		err := log.Open(config.Log.Target)
		if err != nil {
			log.Fatalf("Unable to open log %s: %s", config.Log.Target, err.Error())
		}
	}
	log.Info("config loaded.")
	log.Info("server listen on: " + config.Listen_addr)
	log.Infof("%d upstream server(s) found", len(config.Upstream))
}