Example #1
0
func (a *AuthServer) HandleLogin(w http.ResponseWriter, r *http.Request) {
	var err error
	if config.IsValidClient(r.RemoteAddr) {
		timeout := r.FormValue("timeout")
		nas := r.FormValue("nasip")
		userip_str := r.FormValue("userip")
		username := []byte(r.FormValue("username"))
		userpwd := []byte(r.FormValue("userpwd"))
		var to uint64
		to, err = strconv.ParseUint(timeout, 10, 32)

		userip := net.ParseIP(userip_str)
		if userip == nil {
			if *config.UseRemoteIpAsUserIp == true {
				ip, _, _ := net.SplitHostPort(r.RemoteAddr)
				userip = net.ParseIP(ip)
			} else {
				err = fmt.Errorf("UserIp is not available and UseRemoteIpAsUserIp is false")
			}
		}

		if userip != nil {
			if basip := net.ParseIP(nas); basip != nil {
				log.Printf("got a login request from %s on nas %s\n", userip, basip)
				if len(username) == 0 {
					log.Println("username len = 0")

					if *config.RandomUser {
						username, userpwd = a.RandomUser(userip, basip, *config.HuaweiDomain, uint32(to))
					} else {
						w.WriteHeader(http.StatusBadRequest)
						return
					}
				} else {
					username = []byte(string(username) + "@" + *config.HuaweiDomain)
					a.authing_user[userip.String()] = AuthInfo{username, userpwd, uint32(to)}
				}
				if err = component.Auth(userip, basip, uint32(to), username, userpwd); err == nil {
					w.WriteHeader(http.StatusOK)
					return
				}
			} else {
				err = fmt.Errorf("Parse Ip err from %s", nas)
			}
		}
	} else {
		err = fmt.Errorf("Not Allowed from this IP")
	}
	w.WriteHeader(http.StatusBadRequest)
	w.Write([]byte(err.Error()))
}
Example #2
0
func (a *AuthServer) HandleLogin(w http.ResponseWriter, r *http.Request) {
	var err error
	if config.IsValidClient(r.RemoteAddr) {
		timeout := r.FormValue("timeout")
		nas := r.FormValue("nasip")
		if *config.NasIp != "" {
			nas = *config.NasIp
		}
		userip_str := r.FormValue("userip")
		username := []byte(r.FormValue("username"))
		userpwd := []byte(r.FormValue("userpwd"))
		var to uint64
		to, err = strconv.ParseUint(timeout, 10, 32)

		if to == 0 && *config.DefaultTimeout != 0 {
			to = *config.DefaultTimeout
		}

		userip := net.ParseIP(userip_str)
		if *config.UseRemoteIpAsUserIp == true {
			ip, _, _ := net.SplitHostPort(r.RemoteAddr)
			userip = net.ParseIP(ip)
		} else if userip == nil {
			u_refer := r.Header.Get("Referer")
			var u *url.URL
			if u, err = url.Parse(u_refer); err == nil {
				if uip := u.Query().Get("userip"); uip != "" {
					userip = net.ParseIP(userip_str)
				} else {
					err = fmt.Errorf("请求解析Referer错误")
				}
			} else {
				err = fmt.Errorf("配置错误!请联系管理员")
			}
		}
		var full_username []byte
		if userip != nil {
			if basip := net.ParseIP(nas); basip != nil {
				log.Printf("got a login request from %s on nas %s\n", userip, basip)
				if len(username) == 0 {
					if *config.RandomUser {
						full_username, userpwd = a.RandomUser(userip, basip, *config.HuaweiDomain, uint32(to))
					} else {
						w.WriteHeader(http.StatusBadRequest)
						return
					}
				} else {
					full_username = []byte(string(username) + "@" + *config.HuaweiDomain)
					a.authing_user[userip.String()] = &AuthInfo{username, userpwd, []byte{}, uint32(to)}
				}
				if err = Auth(userip, basip, uint32(to), []byte(full_username), userpwd); err == nil {
					w.Write([]byte(a.authing_user[userip.String()].Mac.String()))
					return
				}
			} else {
				err = fmt.Errorf("NAS IP配置错误")
			}
		}
	} else {
		err = fmt.Errorf("该IP不在配置可允许的用户中")
	}
	if err != nil {
		log.Println("login error: ", err)
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte(err.Error()))
	}
}