Ejemplo n.º 1
0
func GetUserByName(userName string) (api.User, error) {
	if CData.Users == nil {
		return nil, logger.Err("No registered users")
	}
	for i := range CData.Users {
		if CData.Users[i].GetName() == userName {
			return CData.Users[i], nil
		}
	}
	return nil, logger.Err("User not found")
}
Ejemplo n.º 2
0
func ProcessLogin(cmd *Cmd) error {
	switch len(cmd.Arg) {
	case 1:
		if cmd.Arg[0] == "" {
			return logger.Err("Login: Invalid Name")
		}
		// Currently no real checks
		return nil
	default:
		return logger.Err("Login: Wrong number of arguments needed to login")
	}
	return logger.Err("Internal error for ProcessLogin")
}
Ejemplo n.º 3
0
func (mcp *GoodThopter) writeByte(b uint8) {
	data := make([]byte, b)
	_, err := mcp.Serial.Write(data)
	if err != nil {
		logger.Err("Could not write to Thopter")
	}
}
Ejemplo n.º 4
0
// ProcessIncoming unmarshals string request back to structs
func (c *Client) ProcessIncoming(msg string) (*api.Cmd, error) {
	cmd := &api.Cmd{}
	switch c.Lang {
	case LANG_JSON:
		err := json.Unmarshal([]byte(msg), &cmd)
		if err != nil {
			return cmd, logger.Err("Invalid JSON Syntax")
		}
	default: // LANG_XML
		err := xml.Unmarshal([]byte(msg), &cmd)
		if err != nil {
			return cmd, logger.Err("Invalid XML Syntax")
		}
	}
	return cmd, nil
}
Ejemplo n.º 5
0
func StartSPAWebListener(root string, ip string, port string) error {
	web_root = root
	r := mux.NewRouter()
	r.HandleFunc("/", homeHandler)
	r.HandleFunc("/login", loginHandler)
	r.HandleFunc("/logout", logoutHandler)
	r.HandleFunc("/partials/lobby.html", partialLobbyHandler)
	r.HandleFunc("/candevice/{id}/config", configCanHandler)
	r.HandleFunc("/candevice/{id}/join", joinHaxHandler)
	r.HandleFunc("/candevice/{id}/info", candeviceInfoHandler)
	r.HandleFunc("/hax/{id}/packets", haxPacketsHandler)
	r.HandleFunc("/hax/{id}/start", haxStartHandler)
	r.HandleFunc("/hax/{id}/stop", haxStopHandler)
	r.HandleFunc("/hax/{id}/transmit", haxTransmitHandler)
	r.HandleFunc("/candevices", candevicesHandler)
	r.HandleFunc("/lobby/AddSimulator", addSimHandler)

	http.Handle("/partials/", http.FileServer(http.Dir(root)))
	http.Handle("/js/", http.FileServer(http.Dir(root)))
	http.Handle("/css/", http.FileServer(http.Dir(root)))
	http.Handle("/fonts/", http.FileServer(http.Dir(root)))
	http.Handle("/images/", http.FileServer(http.Dir(root)))
	http.Handle("/bootstrap/", http.FileServer(http.Dir(root)))
	http.Handle("/", r)
	remote := ip + ":" + port
	logger.Log("Starting CANiBUS Web server on " + remote)
	err := http.ListenAndServe(remote, nil)
	if err != nil {
		return logger.Err("Could not bind web to port: " + err.Error())
	}
	return nil
}
Ejemplo n.º 6
0
func checkAuth(w http.ResponseWriter, r *http.Request) error {
	session, _ := store.Get(r, "canibus")
	if session.Values["user"] == nil {
		http.Redirect(w, r, "/", http.StatusFound)
		return logger.Err("Not authenticated")
	}
	return nil
}
Ejemplo n.º 7
0
func GetDeviceById(id int) (api.CanDevice, error) {
	drivers := CData.CConfig.GetDrivers()
	for i := range drivers {
		if drivers[i].GetId() == id {
			return drivers[i], nil
		}
	}
	return nil, logger.Err("No device with that ID")
}
Ejemplo n.º 8
0
func (mcp *GoodThopter) readByte() uint8 {
	buf := make([]byte, 128)
	_, err := mcp.Serial.Read(buf)
	if err != nil {
		logger.Err("Could not read from Thopter")
		return 0
	}
	return buf[0]
}
Ejemplo n.º 9
0
func StartWebListener(root string, ip string, port string) error {
	web_root = root
	http.HandleFunc("/", rootHandler)
	http.HandleFunc("/login", loginHandler)
	http.HandleFunc("/logout", logoutHandler)
	http.HandleFunc("/lobby", lobbyHandler)
	http.HandleFunc("/config", configHandler)
	http.HandleFunc("/hacksession", hacksessionHandler)
	//http.Handle("/chatLobby", websocket.Handler(chatLobbyHandler))
	remote := ip + ":" + port
	logger.Log("Starting CANiBUS Web server on " + remote)
	err := http.ListenAndServe(remote, nil)
	if err != nil {
		return logger.Err("Could not bind web to port: " + err.Error())
	}
	return nil
}
Ejemplo n.º 10
0
func (s *HackSession) InjectPacket(user api.User, TxPkt api.TransmitPacket) error {
	if s.Device == nil {
		return logger.Err("Device not set")
	}
	var err error
	pkt := api.CanData{}
	pkt.Src = user.GetName()
	pkt.ArbID = TxPkt.ArbId
	pkt.Network = TxPkt.Network
	pkt.B1, err = api.Atoui8(TxPkt.B1)
	if err != nil {
		return err
	}
	pkt.B2, err = api.Atoui8(TxPkt.B2)
	if err != nil {
		return err
	}
	pkt.B3, err = api.Atoui8(TxPkt.B3)
	if err != nil {
		return err
	}
	pkt.B4, err = api.Atoui8(TxPkt.B4)
	if err != nil {
		return err
	}
	pkt.B5, err = api.Atoui8(TxPkt.B5)
	if err != nil {
		return err
	}
	pkt.B6, err = api.Atoui8(TxPkt.B6)
	if err != nil {
		return err
	}
	pkt.B7, err = api.Atoui8(TxPkt.B7)
	if err != nil {
		return err
	}
	pkt.B8, err = api.Atoui8(TxPkt.B8)
	if err != nil {
		return err
	}
	err = s.Device.InjectPacket(pkt)
	return err
}
Ejemplo n.º 11
0
// StartListener starts a listening socket on a given port and IP
func StartListener(ip string, port string) error {
	serverInit()
	remote := ip + ":" + port
	logger.Log("Starting CANiBUS Server on " + remote)
	ln, err := net.Listen("tcp", remote)
	if err != nil {
		return logger.Err("Could not bind to port: " + err.Error())
	}
	logger.Log("Server started")
	defer ln.Close()
	for {
		conn, err := ln.Accept()
		if err != nil {
			fmt.Println(err)
			continue
		}
		go handleConnection(conn)
	}
	return nil
}
Ejemplo n.º 12
0
// Sends a command then polls until it recieves a prompt
func (e *Elm327) SendCmd(line string) ([]string, error) {
	fmt.Println("DEBUG: Sending command: ", line)
	e.Serial.Writeln(line)
	timeout := 10
	maxlines := 5
	var result []string
	done := false
	for timeout > 0 && !done {
		timeout -= 1
		resp, gotLine := e.Serial.ReadLn()
		fmt.Println("DEBUG: gotLine", gotLine)
		if gotLine {
			maxlines -= 1
		} else {
			timeout -= 1
			if e.Serial.GotPrompt {
				done = true
			}
		}
		if maxlines == 0 {
			done = true
		}
		// Ignore ECHO
		if line != resp {
			fmt.Println("DEBUG: SendCmd resp=", resp)
			if len(resp) > 1 {
				result = append(result, string(resp))
			}
		}
	}
	fmt.Println("SendCmd Results: ", result)
	if len(result) > 1 && strings.Contains(result[1], "UNABLE TO CONNECT") {
		return nil, logger.Err("Unable to Connect to ECU")
	}
	return result, nil
}