// NewTF2RconConnection builds a new TF2RconConnection to a server at address ("ip:port") using
// a rcon_password password
func NewTF2RconConnection(address, password string) (*TF2RconConnection, error) {
	rc, err := rcon.Dial(address, password)
	if err != nil {
		return nil, err
	}
	return &TF2RconConnection{rc, address}, nil
}
Example #2
0
func NewARKRconConnection(address, password string) (*ARKRcon, error) {
	var err error
	var rc *rcon.RemoteConsole

	if rc, err = rcon.Dial(address, password); err != nil {
		return nil, err
	}
	return &ARKRcon{rc, address, password}, nil
}
// ChangeRconPassword changes the rcon password and updates the current connection
// to use the new password
func (c *TF2RconConnection) ChangeRconPassword(password string) error {
	_, err := c.SetConVar("rcon_password", password)

	if err == nil {
		c.rc.Close()
		newConnection, _ := rcon.Dial(c.host, password)
		c.rc = newConnection
	}

	return err
}
// ChangeRconPassword changes the rcon password and updates the current connection
// to use the new password
func (c *TF2RconConnection) ChangeRconPassword(password string) error {
	query := "rcon_password \"" + password + "\""
	_, err := c.Query(query)

	if err == nil {
		c.rc.Close()
		newConnection, _ := rcon.Dial(c.host, password)
		c.rc = newConnection
	}

	return err
}
Example #5
0
func NewRustServer(config *RustServerConfig) (*RustServer, error) {
	srv := &RustServer{
		config: config,
	}

	// allow nil config to be passed; i.e. to enable starting
	// the initial rust server
	if config != nil {
		c, err := rcon.Dial(config.RconAddress, config.RconPassword)
		if err != nil {
			return nil, fmt.Errorf("error connecting to rcon: %s", err)
		}

		respChan := make(chan int)
		errChan := make(chan error)

		// error reporter channel
		go func() {
			for err := range errChan {
				log.Error(err)
			}
		}()

		srv.console = c
		srv.requests = map[int]string{}
		srv.respChan = respChan
		srv.errChan = errChan

		ticker := time.NewTicker(time.Millisecond * 250)
		// read channel for rcon
		go func() {
			// start read loop
			for _ = range ticker.C {
				resp, id, err := srv.console.Read()
				if err != nil {
					errChan <- err
				}

				srv.requests[id] = resp
				respChan <- id
			}

		}()
	}

	return srv, nil
}
Example #6
0
File: rcon.go Project: Xe/xeserv.us
func Query(host, password string) (*Status, error) {
	rc, err := rcon.Dial(host, password)
	if err != nil {
		return nil, err
	}
	defer rc.Close()

	id, err := rc.Write("status")
	if err != nil {
		return nil, err
	}

	reply, theirID, err := rc.Read()
	if err != nil {
		return nil, err
	}

	if id != theirID {
		return nil, errors.New("tf2: things got out of order somehow, I'm gonna stop trying.")
	}

	s := &Status{
		Online: true,
	}

	lines := strings.Split(reply, "\n")

	for _, line := range lines {
		fields := strings.Fields(line)

		if len(fields) == 0 {
			continue
		}

		switch fields[0] {
		case "hostname:":
			s.Hostname = strings.Join(fields[1:], " ")

		case "version":
			s.Version = strings.Join(fields[2:], " ")

		case "map":
			s.MapName = fields[2]

		case "tags":
			s.Tags = fields[2]

		case "players":
			var err error

			s.HumanPlayers, err = strconv.Atoi(fields[2])
			if err != nil {
				return nil, err
			}

			s.Bots, err = strconv.Atoi(fields[4])
			if err != nil {
				return nil, err
			}

			s.MaxPlayers, err = strconv.Atoi(fields[6][1:])
			if err != nil {
				return nil, err
			}

		case "#":
			if fields[1] == "userid" {
				continue
			}

			info, err := shlex.Split(line, true)
			if err != nil {
				return nil, err
			}

			var p Player

			// I'm sorry, really

			if info[3] == "BOT" {
				p = Player{
					UserID:   info[1],
					Name:     info[2],
					UniqueID: info[3],
					State:    info[4],
					IsBot:    true,
				}
			} else {
				p = Player{
					UserID:    info[1],
					Name:      info[2],
					UniqueID:  info[3],
					Connected: info[4],
					State:     info[7],
					Address:   info[8],
					IsBot:     false,
				}
			}

			s.Players = append(s.Players, p)
		}
	}

	return s, nil
}