Ejemplo n.º 1
0
func runClient(conf *Config) error {
	if *udp {
		return runUDP(conf)
	}
	hub.Init()
	test := net.ParseIP(conf.Server.Ip)
	v6 := false
	if test.To4() == nil {
		v6 = true
	}
	cipher, err := ss.NewCipher(conf.User.Method, conf.User.Password)
	if err != nil {
		return err
	}
	ifce, err := tap.NewTAP()
	if err != nil {
		log.Println("Failed to create TAP device:", err)
		return err
	}
	defer ifce.Close()
	local := &Client{MacAddr: ifce.MacAddr(), Conn: ifce}
	local.Init()
	hub.Connect(local)
	if !v6 {
		ip, ip_mask, err := net.ParseCIDR(conf.Server.Ip + "/32")
		if err != nil {
			log.Println("Failed to parse ip:", err)
			return err
		}
		ip_mask.IP = ip
		err = tap.Bypass(ip_mask)
		if err != nil {
			log.Println("[Client]: Failed to bypass server address from route, please manually fix that:", err)
		}
		defer tap.Unbypass()
	}
	// reg with server
	var dst string
	if v6 {
		dst = fmt.Sprintf("[%s]:%d", conf.Server.Ip, conf.Server.Port)
	} else {
		dst = fmt.Sprintf("%s:%d", conf.Server.Ip, conf.Server.Port)
	}
	conn, err := net.Dial("tcp", dst)
	if err != nil {
		return err
	}
	var c net.Conn
	if *enc {
		c = ss.NewPacketStreamConn(conn)
	} else {
		secureconn := ss.NewConn(conn, cipher.Copy())
		c = ss.NewPacketStreamConn(secureconn)
	}
	defer c.Close()
	auth := new(Auth)
	auth.Type = Auth_Hello
	mac := ifce.MacAddr()
	auth.MacAddr = mac[:]
	data, err := auth.Marshal()
	if err != nil {
		log.Println("[Client]: Failed to marshal data:", err)
		return err
	}
	_, err = c.Write(data)
	if err != nil {
		log.Println("[Client]: Failed to write socket: ", err)
		return err
	}
	buf := make([]byte, 2048)
	n, err := c.Read(buf)
	if err != nil {
		log.Println("[Client]: Failed to recieve config: ", err)
		return err
	}
	err = auth.Unmarshal(buf[:n])
	if err != nil {
		log.Println("[Client]: Failed to decode recieved config: ", err)
		return err
	}
	if auth.Type != Auth_Welcome {
		return fmt.Errorf("[Client]: Unexpected response type: %s.", Auth_MessageType_name[int32(auth.Type)])
	}
	ip, ip_mask, err := net.ParseCIDR(auth.IP)
	if err != nil {
		log.Println("[Client]: Failed to parse CIDR from response:", err)
		return err
	}
	ip_mask.IP = ip
	err = ifce.SetIP(ip_mask)
	if err != nil {
		log.Println("Failed to set IP address:", err)
		log.Println("Maybe you can manually fix that, so go on.")
		//return err
	}
	ip, ip_mask, err = net.ParseCIDR("0.0.0.0/1")
	if err != nil {
		log.Println("Failed to parse address:", err)
	}
	ip_mask.IP = ip
	ip = net.ParseIP(auth.GateWay)
	err = ifce.AddRoute(ip, ip_mask)
	if err != nil {
		log.Println("Failed to set default route, please manually fix that:", err)
	}
	ip, ip_mask, err = net.ParseCIDR("128.0.0.0/1")
	if err != nil {
		log.Println("Failed to parse address:", err)
	}
	ip_mask.IP = ip
	ip = net.ParseIP(auth.GateWay)
	err = ifce.AddRoute(ip, ip_mask)
	if err != nil {
		log.Println("Failed to set default route, please manually fix that", err)
	}
	defer func() {
		ip, ip_mask, _ = net.ParseCIDR("0.0.0.0/1")
		ip_mask.IP = ip
		ip = net.ParseIP(auth.GateWay)
		ifce.DelRoute(ip, ip_mask)
		ip, ip_mask, _ = net.ParseCIDR("128.0.0.0/1")
		ip_mask.IP = ip
		ip = net.ParseIP(auth.GateWay)
		ifce.DelRoute(ip, ip_mask)
	}()
	client := &Client{MacAddr: BroadcastAddr, Conn: c}
	hub.Connect(client)
	return client.Run()
}
Ejemplo n.º 2
0
func serve(conf *Config, localHWAddr tap.HwAddr) error {
	cipher, err := ss.NewCipher(conf.User.Method, conf.User.Password)
	if err != nil {
		return err
	}
	test := net.ParseIP(conf.Server.Ip)
	v6 := false
	if test.To4() == nil {
		v6 = true
	}
	var addr string
	if v6 {
		addr = fmt.Sprintf("[%s]:%d", conf.Server.Ip, conf.Server.Port)
	} else {
		addr = fmt.Sprintf("%s:%d", conf.Server.Ip, conf.Server.Port)
	}
	listener, err := net.Listen("tcp", addr)
	if err != nil {
		return err
	}
	defer listener.Close()
	log.Printf("Server inited, listen on : %s:%d.\n", conf.Server.Ip, conf.Server.Port)
	// listener handler
	{
		for {
			conn, err := listener.Accept()
			if err != nil {
				return err
			}
			var c net.Conn
			if *enc {
				c = ss.NewPacketStreamConn(conn)
			} else {
				secureconn := ss.NewConn(conn, cipher.Copy())
				c = ss.NewPacketStreamConn(secureconn)
			}
			// conn handle
			go func(conn net.Conn) {
				buf := make([]byte, MaxPacketSize)
				auth := new(Auth)
				response := new(Auth)
				var mac tap.HwAddr
				// err happens, close the connection.
				defer func() {
					if err := recover(); err != nil {
						conn.Close()
					}
				}()
				{
					n, err := conn.Read(buf)
					if err != nil {
						panic(fmt.Sprintln("[Read]: Failed to read: ", err))
					}
					err = auth.Unmarshal(buf[:n])
					if err != nil {
						panic(fmt.Sprintln("[Read]: Failed to Unmarshal data:", err))
					}
					if auth.Type != Auth_Hello {
						panic(fmt.Sprintln("[Read]: Unexpected message type."))
					}
					copy(mac[:], auth.MacAddr[:6])
					response.Type = Auth_Welcome
					response.IP = "192.168.1.101/24"
					response.DNS = "8.8.8.8"
					response.MTU = int32(1500)
					response.GateWay = "192.168.1.1"
					response.MacAddr = localHWAddr[:]
					data, err := response.Marshal()
					if err != nil {
						panic(fmt.Sprintln("[Read]: Failed to marshal response: ", err))
					}
					client := &Client{MacAddr: mac, Conn: conn}
					client.Write(data)
					hub.Connect(client)
					client.Init()
				}
				return
			}(c)
		}
	}
	return nil
}