检测从服务器

时间:2015-04-29 17:32:36

标签: javascript asp.net

客户使用从我的服务器安装脚本(javascript)的服务。我通过脚本将一个跟踪像素插入页面,该脚本可以获取点击信息。

当asp.net服务器拦截pixel.aspx时,它会将伪图像的查询字符串中的登录信息插入数据库并返回pixel.gif ...这一切都完美无缺(见下文)。

Imports Microsoft.VisualBasic
Imports System.Web

Public Class HelloWorldModule
Implements IHttpModule
Private pattern As String = "/images/(?<key>.*)\.aspx"
Private logoFile As String = "~/images/pixel.gif"

Public Sub New()
End Sub

Public ReadOnly Property ModuleName() As String
    Get
        Return "HelloWorldModule"
    End Get
End Property

' In the Init function, register for HttpApplication 
' events by adding your handlers. 
Public Sub Init(ByVal application As HttpApplication) Implements IHttpModule.Init
    AddHandler application.BeginRequest, AddressOf GetImage_BeginRequest
End Sub

Public Sub GetImage_BeginRequest(ByVal sender As Object, ByVal args As System.EventArgs)
    'cast the sender to a HttpApplication object
    Dim application As System.Web.HttpApplication = CType(sender, System.Web.HttpApplication)

    Dim url As String = application.Request.Path 'get the url path
    'create the regex to match for becon images
    Dim r As New Regex(pattern, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
    If r.IsMatch(url) Then
        Dim mc As MatchCollection = r.Matches(url)
        If Not (mc Is Nothing) And mc.Count > 0 Then
            Dim key As String = mc(0).Groups("key").Value
            'SaveToDB(key)
        End If

        'now send the image to the client
        application.Response.ContentType = "image/gif"
        application.Response.WriteFile(application.Request.MapPath(logoFile))
        application.Response.End()
    End If
End Sub 'GetImage_BeginRequest

如果运行此脚本的客户端有一个已过期的帐户,我想在响应中返回error.gif(而不是pixel.gif),然后我需要检测error.gif src所以我可以通知客户端该服务已过期。

我尝试了以下操作,但它返回了图像的原始src。如何检测图像的更新src?

任何帮助或建议都将不胜感激。

testImage("images/pixel.aspx?login=123&country=canada", function (url, result) { if (result === 'success') { console.log(url) } }, 10000);

     function testImage(url, callback, timeout) {
        timeout = timeout || 5000;
        var timedOut = false, timer;
        var img = new Image();
        img.onerror = img.onabort = function () {
            if (!timedOut) {
                clearTimeout(timer);
                callback(url, "error");
            }
        };
        img.onload = function () {
            if (!timedOut) {
                clearTimeout(timer);
                callback(url, "success");
                console.log(checkURL(url))
            }
        };
        img.src = url;
        document.body.appendChild(img);

        timer = setTimeout(function () {
            timedOut = true;
            callback(url, "timeout");
        }, timeout);
    }

 function checkURL(url) {
        return (url.match(/\.(jpeg|jpg|gif|png)$/) != null);
    }

1 个答案:

答案 0 :(得分:0)

由于服务器在页面呈现之前发送pixel.gif,因此没有找到可以识别图像src的内容,因此没有脚本可以检测到src更改。我把逻辑放在服务器上,以便当客户帐户过期时,我现在在请求网页上显示一条消息到期...

' In the Init function, register for HttpApplication 
' events by adding your handlers. 
Public Sub Init(ByVal application As HttpApplication) Implements IHttpModule.Init
    If acct_expired = False Then
        AddHandler application.BeginRequest, AddressOf GetImage_BeginRequest
    Else
        AddHandler application.BeginRequest, AddressOf Application_BeginRequest ' GetImage_BeginRequest
    End If

End Sub

Public Sub GetImage_BeginRequest(ByVal sender As Object, ByVal args As System.EventArgs)
    'cast the sender to a HttpApplication object
    Dim application As System.Web.HttpApplication = CType(sender, System.Web.HttpApplication)

    Dim url As String = application.Request.Path 'get the url path
    'create the regex to match for becon images
    Dim r As New Regex(pattern, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
    If r.IsMatch(url) Then
        Dim mc As MatchCollection = r.Matches(url)
        If Not (mc Is Nothing) And mc.Count > 0 Then
            Dim key As String = mc(0).Groups("key").Value
            'SaveToDB(key)
        End If

        'now send the image to the client
        application.Response.ContentType = "image/gif"
        application.Response.WriteFile(application.Request.MapPath(logoFile))
        application.Response.End()

    End If
End Sub 'GetImage_BeginRequest

Private Sub Application_BeginRequest(ByVal source As Object, _
         ByVal e As EventArgs)
    ' Create HttpApplication and HttpContext objects to access 
    ' request and response properties. 
    Dim application As HttpApplication = DirectCast(source, HttpApplication)
    Dim context As HttpContext = application.Context
    Dim filePath As String = context.Request.FilePath
    Dim fileExtension As String = VirtualPathUtility.GetExtension(filePath)
    context.Response.Write("<hr><h1><font color=red>Your account has expired. Upgrade Today!</font></h1>")
End Sub
相关问题