コード例 #1
0
ファイル: hello.go プロジェクト: kevinburke/snapchat-friends
func api(ctx *web.Context, username string) string {
	user, err := getUser(username)
	ctx.SetHeader("Content-Type", "application/prs.kevinburke.snapchat-v1+json", true)
	if err != nil {
		checkError(err)
		ctx.NotFound("User not found")
		return ""
	}
	friends, err := getFriendsById(user.Id)
	checkError(err)
	var links Links
	var users []User
	users = append(users, *user)
	for i := 0; i < len(friends.Friends); i++ {
		friend := friends.Friends[i]
		link := Link{friend.UserId, friend.FriendId, friend.Index}
		links.Links = append(links.Links, link)
		user, err := getUserById(friend.UserId)
		checkError(err)
		users = append(users, *user)
	}
	response := Response{links.Links, users}
	bytes, err := json.Marshal(response)
	checkError(err)
	return string(bytes)
}
コード例 #2
0
ファイル: fileproxy.go プロジェクト: nakamuray/fileproxy
func downloader(ctx *web.Context, key string) {
	updateRemoteAddr(ctx)

	up, ok := uploadRequests[key]

	if !ok {
		ctx.NotFound("key doesn't exist")
		return
	}

	up.wait <- "connected"

	result := "ng"
	defer func() { up.wait <- result }()

	mr, err := up.request.MultipartReader()
	if err != nil {
		ctx.Abort(500, err.Error())
		return
	}

	p, err := mr.NextPart()
	if p.FormName() == "size" {
		fileSize, err := ioutil.ReadAll(p)
		if err == nil {
			fileSize, err := strconv.ParseInt(string(fileSize), 10, 64)
			if err == nil && fileSize >= 0 {
				ctx.SetHeader("Content-Length", strconv.FormatInt(fileSize, 10), true)
			}
		}
	}

	p, err = mr.NextPart()
	if err != nil {
		ctx.Abort(500, err.Error())
		return
	}
	if p.FormName() != "file" {
		ctx.Abort(500, "invalid POST (upload) request")
		return
	}

	if contentType := p.Header.Get("Content-Type"); contentType != "" {
		ctx.SetHeader("Content-Type", contentType, true)
	}

	ctx.SetHeader("Content-Disposition", "attachment; filename="+p.FileName(), true)

	_, err = io.Copy(ctx, p)

	if err == nil {
		result = "ok"
	} else {
		// XXX: may expose too many infomation (such as client IP address)
		//result = err.Error()
	}
}
コード例 #3
0
func dnsmasq(ctx *web.Context, action string, ip string) string {
	actions := map[string]bool{
		"add":  true,
		"del":  true,
		"old":  true,
		"init": false,
		"tftp": false,
	}
	response := ""

	if actions[action] {
		if !kerberos() {
			log.Printf("Unable to process %s", action)
			ctx.NotFound("That sucks")
			return ""
		}

		tv := Nsupdate{
			Server: cfg.Dns.Nsrvs,
			Ip:     ip,
			Host:   ctx.Params["host"],
			Domain: ctx.Params["domain"],
			Ttl:    cfg.Dns.Ttl,
			Mac:    ctx.Params["mac"],
		}

		IP := net.ParseIP(ip)
		if IP.To4() == nil && IP.To16() != nil {
			tv.Rr = "AAA"
			var expanded string
			for u := 0; u < len(IP); u++ {
				x := fmt.Sprintf("%x", IP[u])
				if len(x) != 2 {
					expanded += "0"
				}
				expanded += x
				if u%2 != 0 && u < (len(IP)-1) {
					expanded += ":"
				}
			}
			tv.Ip = fmt.Sprintf("%s", expanded)
			for _, r := range reverse(expanded) {
				c := string(r)
				if c != ":" {
					tv.Ptr += fmt.Sprintf("%s.", c)
				}
			}
			tv.Ptr += "ipv6.arpa"
		} else {
			s := strings.Split(ip, ".")
			tv.Rr = "A"
			tv.Ptr = fmt.Sprintf("%s.%s.%s.%s.in-addr.arpa", s[3], s[2], s[1], s[0])
		}

		if len(tv.Host) < 2 {
			log.Print("nil host!")
			addr, _ := net.LookupAddr(ip)
			if addr != nil && cfg.Service.Debug {
				log.Printf("LookupAddr: %d%s", len(addr), addr)
			}
		}

		var output bytes.Buffer
		var stderr bytes.Buffer

		nsupdate := exec.Command("nsupdate", cfg.Dns.Nsupdateflags)
		nsin, err := nsupdate.StdinPipe()
		if err != nil {
			log.Fatalf("unable to create pipe: %s", err)
		}
		nsupdate.Stdout, nsupdate.Stderr = &output, &stderr

		nsupdate.Start()

		if action == "add" || action == "old" {
			err = tmpl.Execute(nsin, tv)
			if err != nil {
				log.Fatalf("template execution failed: %s", err)
			}

			response = fmt.Sprintf("%s %s\n", ip, action)
		}

		if action == "del" {
			err := tmpl.ExecuteTemplate(os.Stderr, "dhcplistener.nsupdate.del", tv)
			if err != nil {
				log.Fatalf("template execution failed: %s", err)
			}

			response = fmt.Sprintf("%s %s\n", ip, action)
		}

		nsin.Close()
		nsupdate.Wait()

		if cfg.Service.Debug {
			log.Printf("%s", output)
			log.Printf("%s", stderr)
		}

		return response
	}

	ctx.NotFound("That sucks")
	return ""
}