home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / Server_Key3610311192001.psc / frmServer.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-11-19  |  12.4 KB  |  372 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
  4. Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.2#0"; "richtx32.ocx"
  5. Begin VB.Form frmServer 
  6.    BorderStyle     =   1  'Fixed Single
  7.    Caption         =   "Server Licence Administrator"
  8.    ClientHeight    =   4125
  9.    ClientLeft      =   150
  10.    ClientTop       =   435
  11.    ClientWidth     =   8535
  12.    LinkTopic       =   "Form2"
  13.    MaxButton       =   0   'False
  14.    ScaleHeight     =   4125
  15.    ScaleWidth      =   8535
  16.    StartUpPosition =   2  'CenterScreen
  17.    Begin VB.CommandButton cmdClose 
  18.       Caption         =   "&Close Server"
  19.       Height          =   855
  20.       Left            =   6840
  21.       Picture         =   "frmServer.frx":0000
  22.       Style           =   1  'Graphical
  23.       TabIndex        =   5
  24.       Top             =   3120
  25.       Width           =   1575
  26.    End
  27.    Begin VB.Timer Timer1 
  28.       Interval        =   100
  29.       Left            =   2760
  30.       Top             =   3120
  31.    End
  32.    Begin prjLicence.Server Server 
  33.       Left            =   3120
  34.       Top             =   3120
  35.       _ExtentX        =   741
  36.       _ExtentY        =   741
  37.    End
  38.    Begin RichTextLib.RichTextBox rtbOpen 
  39.       Height          =   255
  40.       Left            =   1680
  41.       TabIndex        =   4
  42.       Top             =   4440
  43.       Visible         =   0   'False
  44.       Width           =   1335
  45.       _ExtentX        =   2355
  46.       _ExtentY        =   450
  47.       _Version        =   393217
  48.       Enabled         =   -1  'True
  49.       TextRTF         =   $"frmServer.frx":0442
  50.    End
  51.    Begin VB.Frame Frame2 
  52.       Caption         =   "Server UID"
  53.       Height          =   855
  54.       Left            =   120
  55.       TabIndex        =   2
  56.       Top             =   3120
  57.       Width           =   2535
  58.       Begin VB.TextBox txtUID 
  59.          Height          =   285
  60.          Left            =   120
  61.          Locked          =   -1  'True
  62.          TabIndex        =   3
  63.          Top             =   360
  64.          Width           =   2295
  65.       End
  66.    End
  67.    Begin MSComDlg.CommonDialog cdMain 
  68.       Left            =   960
  69.       Top             =   3600
  70.       _ExtentX        =   847
  71.       _ExtentY        =   847
  72.       _Version        =   393216
  73.    End
  74.    Begin VB.Frame Frame1 
  75.       Caption         =   "Licence Keys"
  76.       Height          =   2895
  77.       Left            =   120
  78.       TabIndex        =   0
  79.       Top             =   120
  80.       Width           =   8295
  81.       Begin MSComctlLib.ListView lvKeys 
  82.          Height          =   2415
  83.          Left            =   120
  84.          TabIndex        =   1
  85.          Top             =   360
  86.          Width           =   8055
  87.          _ExtentX        =   14208
  88.          _ExtentY        =   4260
  89.          View            =   3
  90.          LabelWrap       =   -1  'True
  91.          HideSelection   =   -1  'True
  92.          FullRowSelect   =   -1  'True
  93.          _Version        =   393217
  94.          ForeColor       =   -2147483640
  95.          BackColor       =   -2147483643
  96.          BorderStyle     =   1
  97.          Appearance      =   1
  98.          NumItems        =   4
  99.          BeginProperty ColumnHeader(1) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
  100.             Text            =   "Licence Key"
  101.             Object.Width           =   7937
  102.          EndProperty
  103.          BeginProperty ColumnHeader(2) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
  104.             SubItemIndex    =   1
  105.             Text            =   "No. Licences"
  106.             Object.Width           =   2540
  107.          EndProperty
  108.          BeginProperty ColumnHeader(3) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
  109.             SubItemIndex    =   2
  110.             Text            =   "Free"
  111.             Object.Width           =   2540
  112.          EndProperty
  113.          BeginProperty ColumnHeader(4) {BDD1F052-858B-11D1-B16A-00C0F0283628} 
  114.             SubItemIndex    =   3
  115.             Text            =   "UID"
  116.             Object.Width           =   0
  117.          EndProperty
  118.       End
  119.    End
  120.    Begin VB.Line Line1 
  121.       X1              =   0
  122.       X2              =   8640
  123.       Y1              =   0
  124.       Y2              =   0
  125.    End
  126.    Begin VB.Menu mnuMain 
  127.       Caption         =   "Menu"
  128.       Begin VB.Menu mnuOpenLK 
  129.          Caption         =   "Open Licence Key"
  130.       End
  131.    End
  132. Attribute VB_Name = "frmServer"
  133. Attribute VB_GlobalNameSpace = False
  134. Attribute VB_Creatable = False
  135. Attribute VB_PredeclaredId = True
  136. Attribute VB_Exposed = False
  137. Private Declare Function GetVolumeSerialNumber Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long
  138. Dim iInUse As Integer
  139. Dim s(0 To 255) As Integer 'S-Box
  140. Dim kep(0 To 255) As Integer
  141. 'For the file actions
  142. Dim path As String
  143. Public Sub RC4ini(Pwd As String)
  144.     Dim temp As Integer, a As Integer, b As Integer
  145.     'Save Password in Byte-Array
  146.     b = 0
  147.     For a = 0 To 255
  148.         b = b + 1
  149.         If b > Len(Pwd) Then
  150.             b = 1
  151.         End If
  152.         kep(a) = Asc(Mid$(Pwd, b, 1))
  153.     Next a
  154.     'INI S-Box
  155.     For a = 0 To 255
  156.         s(a) = a
  157.     Next a
  158.     b = 0
  159.     For a = 0 To 255
  160.         b = (b + s(a) + kep(a)) Mod 256
  161.         ' Swap( S(i),S(j) )
  162.         temp = s(a)
  163.         s(a) = s(b)
  164.         s(b) = temp
  165.     Next a
  166. End Sub
  167. Public Function EnDeCrypt(plaintxt As Variant) As Variant
  168.     Dim temp As Integer, a As Long, i As Integer, j As Integer, k As Integer
  169.     Dim cipherby As Byte, cipher As Variant
  170.     For a = 1 To Len(plaintxt)
  171.         i = (i + 1) Mod 256
  172.         j = (j + s(i)) Mod 256
  173.         ' Swap( S(i),S(j) )
  174.         temp = s(i)
  175.         s(i) = s(j)
  176.         s(j) = temp
  177.         'Generate Keybyte k
  178.         k = s((s(i) + s(j)) Mod 256)
  179.         'Plaintextbyte xor Keybyte
  180.         cipherby = Asc(Mid$(plaintxt, a, 1)) Xor k
  181.         cipher = cipher & Chr(cipherby)
  182.     Next a
  183.     EnDeCrypt = cipher
  184. End Function
  185. Private Sub cmdClose_Click()
  186.     Server.StopServer
  187.     Unload Me
  188. End Sub
  189. Private Sub Form_Load()
  190.                
  191.     Dim Hostname As String, IPAdd As String
  192.     'get Server IP and save it to local file
  193.     Hostname = GetIPHostName()
  194.     IPAdd = GetIPAddress()
  195.     rtbOpen.Text = ""
  196.     rtbOpen.Text = IPAdd
  197.     rtbOpen.SaveFile App.path & "\SVRIP.DAT", rtfText
  198.     rtbOpen.Text = ""
  199.     txtUID = Trim(VolumeSerialNumber("C:\"))
  200.     Call Server.StartServer(123, IPAdd)
  201.     iInUse = 0
  202.     OpenLocalKeys
  203. End Sub
  204. Private Sub Form_Terminate()
  205.     On Error Resume Next
  206.     Kill App.path & "\SVRIP.DAT"
  207.     Call Server.StopServer
  208. End Sub
  209. Private Sub Form_Unload(Cancel As Integer)
  210.     On Error Resume Next
  211.     Kill App.path & "\SVRIP.DAT"
  212.     Call Server.StopServer
  213. End Sub
  214. Private Sub mnuOpenLK_Click()
  215.     'I chose .dlk as my licence key extension
  216.     On Error GoTo InvalidKey
  217.     cdMain.Filter = "*.dlk|*.dlk"
  218.     cdMain.FileName = ""
  219.     cdMain.ShowOpen
  220.     'check if a file was selected
  221.     If cdMain.FileName <> "" Then
  222.         'open key
  223.         Dim MyStr As String
  224.         Dim MyCipher As String
  225.         Dim mlen As Integer
  226.         rtbOpen.LoadFile cdMain.FileName, rtfText
  227.         mlen = Left(rtbOpen.Text, 2)
  228.         MyCipher = Mid(rtbOpen.Text, 38, mlen)
  229.         RC4ini (txtUID.Text)
  230.         MyStr = EnDeCrypt(MyCipher)
  231.         
  232.         Dim mynum As Double
  233.         Dim myhex As Long
  234.         Dim myhex2 As String
  235.         
  236.         mynum = Split(MyStr, " ")(0)
  237.         myhex = Split(MyStr, " ")(1)
  238.         myhex2 = Hex(myhex)
  239.         
  240.         a = mynum - Asc(Mid(myhex2, 2, 1))
  241.         b = Asc(Right(myhex2, 1))
  242.         d = Asc(Left(myhex2, 1))
  243.         
  244.         c = a / d
  245.         f = Int(c / b)
  246.         
  247.         'first check that this is not just a renamed file
  248.         'use the unique hex code for this
  249.         
  250.         For i = 1 To lvKeys.ListItems.Count
  251.             If myhex2 = lvKeys.ListItems(i).SubItems(3) Then
  252.                 MsgBox "This key is already registered!", vbExclamation + vbOKOnly, "Error"
  253.                 Exit Sub
  254.             End If
  255.         Next i
  256.         
  257.         'f is the number of licences
  258.         Dim LI As ListItem
  259.         Set LI = lvKeys.ListItems.Add(, , cdMain.FileName)
  260.         LI.SubItems(1) = Str(f)
  261.         LI.SubItems(2) = Str(f)
  262.         LI.SubItems(3) = myhex2
  263.         
  264.     End If
  265.     Exit Sub
  266. InvalidKey:
  267. MsgBox "Key is invalid!", vbExclamation + vbOKOnly, "Error"
  268. End Sub
  269. Private Function VolumeSerialNumber(ByVal RootPath As String) As String
  270. Dim VolLabel As String
  271. Dim VolSize As Long
  272. Dim Serial As Long
  273. Dim MaxLen As Long
  274. Dim Flags As Long
  275. Dim Name As String
  276. Dim NameSize As Long
  277. Dim s As String
  278. Dim ret As Boolean
  279. ret = GetVolumeSerialNumber(RootPath, VolLabel, VolSize, Serial, MaxLen, Flags, Name, NameSize)
  280. If ret Then
  281.     VolumeSerialNumber = Str(Serial)
  282.     VolumeSerialNumber = "00000000"
  283. End If
  284. End Function
  285. Private Sub Server_DataArrival(ByVal SckIndex As Integer, ByVal Data As String, ByVal bytesTotal As Long, ByVal RemoteIP As String, ByVal RemoteHost As String)
  286.     'Call sOutput(FormatNumber(bytesTotal, 0, , , vbTrue) & " bytes recieved.", RemoteIP)
  287.     If Data = "ConReq" Then
  288.         For i = 1 To lvKeys.ListItems.Count
  289.             If Val(lvKeys.ListItems(i).SubItems(2)) > 0 Then
  290.                 lvKeys.ListItems(i).SubItems(2) = Val(lvKeys.ListItems(i).SubItems(2)) - 1
  291.                 Server.SendData "Granted", SckIndex
  292.                 Exit Sub
  293.             End If
  294.         Next i
  295.         Server.SendData "Invalid Command.", SckIndex
  296.     End If
  297.     If Data = "Closing" Then
  298.         For i = 1 To lvKeys.ListItems.Count
  299.             If Val(lvKeys.ListItems(i).SubItems(2)) < Val(lvKeys.ListItems(i).SubItems(1)) Then
  300.                 lvKeys.ListItems(i).SubItems(2) = Val(lvKeys.ListItems(i).SubItems(2)) + 1
  301.                 Exit Sub
  302.             End If
  303.         Next i
  304.     End If
  305. End Sub
  306. Private Sub Timer1_Timer()
  307.     'count connections, hence licences
  308.     If Server.ConnectionCount >= iInUse Then
  309.         iInUse = Server.ConnectionCount
  310.     Else
  311.         For j = 1 To iInUse - Server.ConnectionCount
  312.             For i = 1 To lvKeys.ListItems.Count
  313.                 If Val(lvKeys.ListItems(i).SubItems(2)) < Val(lvKeys.ListItems(i).SubItems(1)) Then
  314.                     lvKeys.ListItems(i).SubItems(2) = Val(lvKeys.ListItems(i).SubItems(2)) + 1
  315.                     Exit For
  316.                 End If
  317.             Next i
  318.         Next j
  319.     End If
  320. End Sub
  321. Private Sub OpenLocalKeys()
  322.         Dim MyStr As String
  323.         Dim MyCipher As String
  324.         Dim mlen As Integer
  325.         
  326.         m = Dir(App.path & "\*.dlk", vbNormal)
  327.         If m = "" Then
  328.             Exit Sub
  329.         End If
  330.         
  331.         Do
  332.             rtbOpen.LoadFile App.path & "\" & m, rtfText
  333.             mlen = Left(rtbOpen.Text, 2)
  334.             MyCipher = Mid(rtbOpen.Text, 38, mlen)
  335.             RC4ini (txtUID.Text)
  336.             MyStr = EnDeCrypt(MyCipher)
  337.             
  338.             Dim mynum As Double
  339.             Dim myhex As Long
  340.             Dim myhex2 As String
  341.             
  342.             mynum = Split(MyStr, " ")(0)
  343.             myhex = Split(MyStr, " ")(1)
  344.             myhex2 = Hex(myhex)
  345.             
  346.             a = mynum - Asc(Mid(myhex2, 2, 1))
  347.             b = Asc(Right(myhex2, 1))
  348.             d = Asc(Left(myhex2, 1))
  349.             
  350.             c = a / d
  351.             f = Int(c / b)
  352.             
  353.             'first check that this is not just a renamed file
  354.             'use the unique hex code for this
  355.             
  356.             For i = 1 To lvKeys.ListItems.Count
  357.                 If myhex2 = lvKeys.ListItems(i).SubItems(3) Then
  358.                     MsgBox "This key is already registered!", vbExclamation + vbOKOnly, "Error"
  359.                     Exit Sub
  360.                 End If
  361.             Next i
  362.             
  363.             'f is the number of licences
  364.             Dim LI As ListItem
  365.             Set LI = lvKeys.ListItems.Add(, , App.path & "\" & m)
  366.             LI.SubItems(1) = Str(f)
  367.             LI.SubItems(2) = Str(f)
  368.             LI.SubItems(3) = myhex2
  369.             m = Dir
  370.         Loop Until m = ""
  371. End Sub
  372.