⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmupnptraversalin.frm

📁 对NAT有详细的总结分析
💻 FRM
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "Mscomctl.ocx"
Begin VB.Form frmUPnPTraversalIn 
   Caption         =   "内网服务穿越NAT"
   ClientHeight    =   3375
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   7110
   LinkTopic       =   "Form2"
   ScaleHeight     =   3375
   ScaleWidth      =   7110
   StartUpPosition =   1  'CenterOwner
   Begin VB.CommandButton cmdCancel 
      Caption         =   "取消穿越"
      Height          =   375
      Left            =   5085
      TabIndex        =   16
      Top             =   2385
      Width           =   1620
   End
   Begin MSComctlLib.StatusBar stbDisplay 
      Align           =   2  'Align Bottom
      Height          =   345
      Left            =   0
      TabIndex        =   15
      Top             =   3030
      Width           =   7110
      _ExtentX        =   12541
      _ExtentY        =   609
      _Version        =   393216
      BeginProperty Panels {8E3867A5-8586-11D1-B16A-00C0F0283628} 
         NumPanels       =   1
         BeginProperty Panel1 {8E3867AB-8586-11D1-B16A-00C0F0283628} 
            AutoSize        =   2
         EndProperty
      EndProperty
   End
   Begin MSWinsockLib.Winsock wskIn 
      Left            =   2835
      Top             =   2362
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
   End
   Begin VB.CommandButton cmdOk 
      Caption         =   "开始穿越"
      Height          =   375
      Left            =   3315
      TabIndex        =   13
      Top             =   2385
      Width           =   1620
   End
   Begin VB.CommandButton cmdQuery 
      Caption         =   "获取NAT外网信息"
      Height          =   375
      Left            =   420
      TabIndex        =   12
      Top             =   2385
      Width           =   1620
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   5
      Left            =   5145
      TabIndex        =   11
      Text            =   "Text1"
      Top             =   1770
      Width           =   1575
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   4
      Left            =   5145
      TabIndex        =   10
      Text            =   "Text1"
      Top             =   1365
      Width           =   1575
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   3
      Left            =   1740
      TabIndex        =   9
      Text            =   "Text1"
      Top             =   1770
      Width           =   1575
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   2
      Left            =   1740
      TabIndex        =   8
      Text            =   "Text1"
      Top             =   1365
      Width           =   1575
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   1
      Left            =   1740
      TabIndex        =   7
      Text            =   "Text1"
      Top             =   975
      Width           =   4965
   End
   Begin VB.TextBox txtService 
      Height          =   285
      Index           =   0
      Left            =   1740
      TabIndex        =   6
      Text            =   "Text1"
      Top             =   570
      Width           =   4950
   End
   Begin VB.Label Label2 
      AutoSize        =   -1  'True
      Caption         =   "请输入以下字段:"
      Height          =   195
      Left            =   420
      TabIndex        =   14
      Top             =   195
      Width           =   1440
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "NAT外网服务端口:"
      Height          =   195
      Index           =   5
      Left            =   3540
      TabIndex        =   5
      Top             =   1815
      Width           =   1590
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "NAT外网服务地址:"
      Height          =   195
      Index           =   4
      Left            =   3540
      TabIndex        =   4
      Top             =   1410
      Width           =   1590
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "服务协议:"
      Height          =   195
      Index           =   1
      Left            =   420
      TabIndex        =   3
      Top             =   1020
      Width           =   900
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "内网服务端口:"
      Height          =   195
      Index           =   3
      Left            =   420
      TabIndex        =   2
      Top             =   1815
      Width           =   1260
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "内网服务地址:"
      Height          =   195
      Index           =   2
      Left            =   420
      TabIndex        =   1
      Top             =   1410
      Width           =   1260
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "服务名:"
      Height          =   195
      Index           =   0
      Left            =   420
      TabIndex        =   0
      Top             =   615
      Width           =   720
   End
End
Attribute VB_Name = "frmUPnPTraversalIn"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

'0.初始化文本框数据
Private Sub Form_Load()
    txtService(0).Text = "ftp服务"
    txtService(1).Text = "TCP" '需要全部大写,否则出错
    txtService(2).Text = wskIn.LocalIP
    txtService(3).Text = 21
    txtService(4).Text = ""
    txtService(5).Text = 621
    
    stbDisplay.Panels(1).Width = stbDisplay.Width
End Sub

'1.查询NAT外网地址
Private Sub cmdQuery_Click()
    txtService(4).Text = GetExternalIP()
End Sub

'2.设置NAT,让外网客户端能主动访问内网客户端某服务
Private Sub cmdOK_Click()
    If CheckInput() = False Then
        Exit Sub
    End If
    
    DisplayMsg "正在设置NAT,穿越中..."
    If SetExternalPort(txtService(5).Text, txtService(1).Text, txtService(3).Text, wskIn.LocalIP, True, Me.Caption + ":" + txtService(0).Text) = True Then
        DisplayMsg "设置NAT成功,穿越成功。外网用户通过" + txtService(4).Text + ":" + txtService(5).Text + "可以访问内网服务了"
    Else
        DisplayMsg "设置NAT失败,穿越失败。"
    End If
End Sub

'3.取消NAT设置,不让外网客户端能主动访问内网客户端某服务
Private Sub cmdCancel_Click()
    DisplayMsg "正在设置NAT,取消穿越中..."
    If DeleteExternalPort(txtService(5).Text, txtService(1).Text) = True Then
        DisplayMsg "设置NAT成功,取消穿越成功。外网用户无法通过" + txtService(4).Text + ":" + txtService(5).Text + "访问内网服务了"
    Else
        DisplayMsg "设置NAT失败,取消穿越失败。"
    End If
End Sub

'改变文本框底色
Private Sub txtService_GotFocus(Index As Integer)
    txtService(Index).BackColor = &H80000018
End Sub

'改变文本框底色
Private Sub txtService_LostFocus(Index As Integer)
    txtService(Index).BackColor = vbWhite
End Sub

'处理文本框中按下回车键
Private Sub txtService_KeyPress(Index As Integer, KeyAscii As Integer)
    Dim intTemp As Integer
    If KeyAscii = 13 Then
        If Index = txtService.UBound Then
            intTemp = txtService.LBound
        Else
            intTemp = Index + 1
        End If
        txtService(intTemp).SetFocus
        KeyAscii = 0
    End If
End Sub

'检查输入
Private Function CheckInput() As Boolean
    CheckInput = True
    Dim i As Integer
    For i = txtService.LBound To txtService.UBound
        If txtService(i).Text = "" Then
            MsgBox "请输入数据到" & Label1(i).Caption
            txtService(i).SetFocus
            CheckInput = False
            Exit Function
        End If
    Next i
    txtService(1).Text = UCase(txtService(1).Text)
    If txtService(1).Text <> "TCP" And txtService(1).Text <> "UDP" Then
        MsgBox "请输入正确数据到" & Label1(1).Caption & "如:TCP、UDP"
        txtService(1).SetFocus
        CheckInput = False
        Exit Function
    End If
'    其他的情况目前没有检查,如IP地址合法性、端口合法性等
End Function

'显示信息
Private Sub DisplayMsg(strMsg As String)
    stbDisplay.Panels(1).Text = strMsg
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -