Beispiel #1
0
func (client *DNSPodClient) GetDomainInfo(domain string, info *DomainInfo) error {
	t, err := client.token(client.email, client.password)
	if err != nil {
		return err
	}

	data := url.Values{}
	data.Set("user_token", t)
	data.Set("domain", domain)
	data.Set("format", "json")

	var ret map[string]interface{}
	u := rpc.BuildHttpsURL(client.addr, "/Domain.Info", nil)
	if err := client.conn.PostForm(nil, u, data, &ret); err != nil {
		return err
	}

	var status Status
	if err := util.Convert(ret["status"], &status); err != nil {
		return err
	}

	if status.Code != "1" {
		return errors.New(status.Message)
	}

	if err := util.Convert(ret["domain"], info); err != nil {
		return err
	}
	return nil
}
Beispiel #2
0
func (vh *VirtualHost) Route(req *http.Request) (reverse.Endpoint, error) {
	if vh.store == nil {
		return nil, fmt.Errorf("virtual hosts not loaded.")
	}
	vh.RLock()
	defer vh.RUnlock()

	hostname := strings.Split(strings.ToLower(req.Host), ":")[0]
	matcher, exists := vh.hosts[hostname]
	if !exists {
		val, err := vh.store.Get(path.Join("/", hostname), true)
		if err != nil {
			return nil, err
		}

		var url string
		if err := util.Convert(val, &url); err != nil {
			return nil, err
		}

		endpoint, err := reverse.ParseUrl(url)
		if err != nil {
			return nil, err
		}

		vh.hosts[hostname] = endpoint
		return endpoint, nil
	}
	return matcher, nil
}
Beispiel #3
0
func (client *DNSPodClient) DelRecord(domainID string, recordID string) error {
	t, err := client.token(client.email, client.password)
	if err != nil {
		return err
	}

	data := url.Values{}
	data.Set("user_token", t)
	data.Set("domain_id", domainID)
	data.Set("record_id", recordID)
	data.Set("format", "json")

	var ret map[string]interface{}
	u := rpc.BuildHttpsURL(client.addr, "/Record.Remove", nil)
	if err := client.conn.PostForm(nil, u, data, &ret); err != nil {
		return err
	}

	var status Status
	if err := util.Convert(ret["status"], &status); err != nil {
		return err
	}

	if status.Code != "1" {
		return errors.New(status.Message)
	}
	return nil
}
Beispiel #4
0
func (client *DNSPodClient) ModRecord(domainID string, info *RecordInfo) error {
	t, err := client.token(client.email, client.password)
	if err != nil {
		return err
	}

	data := url.Values{}
	data.Set("user_token", t)
	data.Set("domain_id", domainID)
	data.Set("sub_domain", info.Name)
	data.Set("record_type", info.Type)
	data.Set("record_line", info.Line)
	data.Set("value", info.Value)
	if info.MX != "" {
		data.Set("mx", info.MX)
	}
	if info.TTL != "" {
		data.Set("ttl", info.TTL)
	}
	data.Set("format", "json")

	var ret map[string]interface{}
	u := rpc.BuildHttpsURL(client.addr, "/Record.Modify", nil)
	if err := client.conn.PostForm(nil, u, data, &ret); err != nil {
		return err
	}

	var status Status
	if err := util.Convert(ret["status"], &status); err != nil {
		return err
	}

	if status.Code != "1" {
		return errors.New(status.Message)
	}

	if err := util.Convert(ret["record"], info); err != nil {
		return err
	}
	return nil
}
Beispiel #5
0
func (client *DNSPodClient) token(login_email, login_password string) (string, error) {
	if client.cache != nil {
		if t, ok := client.cache.Get(DNSPODTOKEN); ok {
			return t.(string), nil
		}
	}

	data := url.Values{}
	data.Set("login_email", login_email)
	data.Set("login_password", login_password)
	data.Set("format", "json")

	var ret map[string]interface{}
	u := rpc.BuildHttpsURL(client.addr, "/Auth", nil)
	if err := client.conn.PostForm(nil, u, data, &ret); err != nil {
		return "", err
	}

	var status Status
	if err := util.Convert(ret["status"], &status); err != nil {
		return "", err
	}

	if status.Code != "1" {
		return "", errors.New(status.Message)
	}

	var token string
	if err := util.Convert(ret["user_token"], &token); err != nil {
		return "", err
	}
	if client.cache != nil {
		client.cache.Set(DNSPODTOKEN, token, time.Minute*10)
	}
	client.email = login_email
	client.password = login_password
	return token, nil
}
Beispiel #6
0
func (ctx *context) get_click(uri string) (int64, error) {
	ctx.RLock()
	defer ctx.RUnlock()

	val, err := ctx.app.clicks.Get(uri, true)
	if err != nil {
		return 0, err
	}

	var click Click
	if err := util.Convert(val, &click); err != nil {
		return 0, err
	}

	return click.Count, nil
}
Beispiel #7
0
func (vh *VirtualHost) GetHost(hostname string) (string, error) {
	if vh.store == nil {
		return "", fmt.Errorf("virtual hosts not loaded.")
	}
	vh.RLock()
	defer vh.RUnlock()

	hostname = strings.ToLower(hostname)
	val, err := vh.store.Get(path.Join("/", hostname), true)
	if err != nil {
		return "", err
	}

	var url string
	if err := util.Convert(val, &url); err != nil {
		return "", err
	}
	return url, nil
}
Beispiel #8
0
func (ctx *context) inc_click(uri string) error {
	ctx.Lock()
	defer ctx.Unlock()

	var click Click
	if val, err := ctx.app.clicks.Get(uri, true); err == nil {

		if err := util.Convert(val, &click); err != nil {
			return err
		}
	}
	click.URI = uri
	click.Count++
	if err := ctx.app.clicks.Put(uri, click); err != nil {
		return err
	}

	return nil
}
Beispiel #9
0
func (ctx *context) clicks_desc(namespace string, n int, suffix string) ([]Click, error) {
	ctx.RLock()
	defer ctx.RUnlock()

	var clicks []Click

	vals, err := ctx.app.clicks.MultiGet(namespace, suffix, true)
	if err != nil {
		return nil, err
	}

	if err := util.Convert(vals, &clicks); err != nil {
		return nil, err
	}

	sort.Sort(ByDesc(clicks))

	if n >= len(clicks) {
		return clicks, nil
	}
	return clicks[0:n], nil
}
Beispiel #10
0
func do_static_get(ctx *context, ctrl *ext.Controller) bool {
	r := ctrl.Request

	if !util.Exist(path.Join(ctx.app.Options.StaticRoot, r.URI())) {
		return false
	}

	ctx.app.Configs.SetSection("third")
	qiniu_enable := ctx.app.Configs.BoolDefault("qiniu.enable", false)
	qiniu_domain := ctx.app.Configs.StringDefault("qiniu.domain", "")

	if qiniu_enable {
		if val, err := ctx.app.systems.Get(path.Join("/qiniu", r.URI()), true); err == nil {
			var key string
			if err := util.Convert(val, &key); err == nil {
				uri := fmt.Sprintf("http://%s", path.Join(qiniu_domain, key))
				ctrl.Redirect(uri, r.URL.RawQuery)
				return true
			}
		}
	}
	ctrl.File(path.Join(ctx.app.Options.StaticRoot, r.URI()))
	return true
}
Beispiel #11
0
func (ns *Namespace) GetValue(uri string, ret interface{}) error {
	if !strings.HasPrefix(uri, ns.namespace) {
		return errors.New(uri + " wrong")
	}
	folders := strings.Split(strings.Trim(strings.TrimPrefix(uri, ns.namespace), "/"), "/")
	var nsloop OBJECT = ns
	for _, folder := range folders {
		if len(folder) == 0 {
			continue
		}
		obj, ok := nsloop.(*Namespace).objects[folder]
		if !ok {
			return errors.New(uri + " wrong")
		}

		if obj.Type() == OBJECT_T {
			return obj.GetValue(uri, ret)
		}

		nsloop = obj
	}

	return util.Convert(nsloop.Value(), ret)
}
Beispiel #12
0
func (obj *Object) GetValue(uri string, ret interface{}) error {
	if uri == path.Join(obj.namespace, obj.key) {
		return util.Convert(obj.value, ret)
	}
	return errors.New(uri + " wrong")
}