如何获取ring-clojure中的客户端IP地址?

时间:2015-05-03 18:56:58

标签: clojure ring

当访问者提交表单时,我想联想输入他的IP地址。

private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        if (CollectionCanceled)
            return;

        //Get data from SQL DB
        var jobService = _serviceFactory.CreateJobService();
        var jobs = jobService.GetAllJobs(true);
        backgroundWorker1.ReportProgress(0, jobs);
    }

    private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        var allJobs = e.UserState as IList<Job>;
        if (allJobs == null || CollectionCanceled)
            return;

        //Build collection
        Dictionary<string, Dictionary<string, int>> details = new Dictionary<string, Dictionary<string, int>>();
        foreach (var job in allJobs)
        {
            var overViewKey = job.State.ToString();
            var detailKey = string.Format("{0} ({1})", job.Name, job.Type);

            if (!details.ContainsKey(overViewKey))
                details.Add(overViewKey, new Dictionary<string, int>());
            if (!details[overViewKey].ContainsKey(detailKey))
                details[overViewKey].Add(detailKey, 0);

            details[overViewKey][detailKey]++;
        }

        //Build charts
        Overview.Series.Clear();
        Detailed.Series.Clear();
        Overview.Series.Add("Jobs");
        Detailed.Series.Add("Jobs");

        foreach (var runniningOrEnqueued in details)
        {
            Overview.Series["Jobs"].Points.AddXY(runniningOrEnqueued.Key, runniningOrEnqueued.Value.Count);
            foreach (var jobDetail in runniningOrEnqueued.Value)
                Detailed.Series["Jobs"].Points.AddXY(jobDetail.Key, jobDetail.Value);
        }
        //Stop for however many secs the user specified 
        Thread.Sleep((int)TimeSpan.FromSeconds(WaitTimer).TotalMilliseconds);
    }

    private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (!CollectionCanceled)
        {
            ExecuteBackGroundWork();
        }
        else
        {
            CollectionCanceled = false;
        }
    }

    private void ExecuteBackGroundWork()
    {
        backgroundWorker1.RunWorkerAsync();
    }

怎么做?

想到这样做:

(POST "/form" {params :params} (assoc params :ip-address the-ip)

但这会返回(POST "/form" {params :params client-ip :remote-addr} (->> params keywordize-keys (merge {:ip-address client-ip}) str))

3 个答案:

答案 0 :(得分:5)

从Arthur Ulfeldt的评论到Bill的回答,我想我们可以写下这个:

$this->input->post('to_date') }

答案 1 :(得分:4)

从请求对象获取:remote-addr通常是正确的方法,除非您坐在负载均衡器后面。在这种情况下,您的负载余额可能会添加一个标题请求x-forwarded-for。这会使这样的事情合适。

(or (get-in request [:headers "x-forwarded-for"]) (:remote-addr request))

答案 2 :(得分:0)

我很确定你可以从响铃请求对象中获取此信息。看看ring.middleware.dump(handle-dump)包。如果你设置它并使用它的简单路线,你会得到一个很好的网页,显示环请求对象中提供的所有可用值。很确定这包括客户端IP地址。由于此信息是从http标头获取的,因此它可能比手动将其放入您的请求参数更可靠 - 将它放入您的请求参数中使用表单或json可能意味着它更容易伪造。此外,如果它已经作为请求对象的一部分提供,那么就意味着你需要做的事情。