iSafer_Option.pas
上传用户:xiuanze55
上传日期:2017-08-03
资源大小:1080k
文件大小:44k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit iSafer_Option;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, ComCtrls, ExtCtrls, StdCtrls, ShellAPI, Registry, IniFiles,
  6.   ScktComp, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  7.   Sockets,StrUtils,IdTrivialFTPBase, Menus, Buttons, Winsock, ImgList;
  8. type
  9.   TForm_Option = class(TForm)
  10.     PageControl1: TPageControl;
  11.     TabSheet_FWRule: TTabSheet;
  12.     TabSheet_Level: TTabSheet;
  13.     TabSheet_FWLog: TTabSheet;
  14.     TabSheet_Folder: TTabSheet;
  15.     GroupBox_Folder: TGroupBox;
  16.     ListView_Folder: TListView;
  17.     GroupBox_FWRule: TGroupBox;
  18.     ListView_FWRule: TListView;
  19.     GroupBox_Level: TGroupBox;
  20.     Label1: TLabel;
  21.     Label_Level0: TLabel;
  22.     Label_Level1: TLabel;
  23.     Label_Level2: TLabel;
  24.     TrackBar_Level: TTrackBar;
  25.     TabSheet_Port: TTabSheet;
  26.     GroupBox_Port: TGroupBox;
  27.     ListView_Port: TListView;
  28.     ListView_PortTemp: TListView;
  29.     Path_List: TListView;
  30.     LogPopupMenu: TPopupMenu;
  31.     mnuAddIPRule: TMenuItem;
  32.     N1: TMenuItem;
  33.     Display1: TMenuItem;
  34.     mnuAddPathRule: TMenuItem;
  35.     N2: TMenuItem;
  36.     mnuDirection: TMenuItem;
  37.     mnuPermission: TMenuItem;
  38.     mnuApplicationPath: TMenuItem;
  39.     mnuBytesReceived: TMenuItem;
  40.     mnuBytesSent: TMenuItem;
  41.     mnuSocketNumber: TMenuItem;
  42.     mnuClearLog: TMenuItem;
  43.     mnuHostName: TMenuItem;
  44.     ProgressBar: TProgressBar;
  45.     BmBtnAddNew: TBitBtn;
  46.     BmtBtnDelete: TBitBtn;
  47.     BmBtnFolderView: TBitBtn;
  48.     BmBtnReload: TBitBtn;
  49.     BmBtnFolderStop: TBitBtn;
  50.     BmBtnFolderInfo: TBitBtn;
  51.     BmBtnPortDefault: TBitBtn;
  52.     BmBtnPortBackDoor: TBitBtn;
  53.     BmBtnPortAll: TBitBtn;
  54.     FWImageList: TImageList;
  55.     BmBtnCancelSecuiry: TBitBtn;
  56.     BmBtnApplySecurity: TBitBtn;
  57.     ImageListForAppPath: TImageList;
  58.     Label2: TLabel;
  59.     Label3: TLabel;
  60.     Label4: TLabel;
  61.     GroupBox1: TGroupBox;
  62.     Label5: TLabel;
  63.     Label_Level3: TLabel;
  64.     GroupBox2: TGroupBox;
  65.     ListView_FWLog: TListView;
  66.     procedure UpdateFolderInfo(Sender: TObject);
  67.     procedure FormCreate(Sender: TObject);
  68.     procedure FormShow(Sender: TObject);
  69.     procedure FormDestroy(Sender: TObject);
  70.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  71.     procedure TabSheet_FolderShow(Sender: TObject);
  72.     // 焊救 荐霖 汲沥 - 包访 窃荐
  73.     procedure TabSheet_LevelShow(Sender: TObject);
  74.     procedure TrackBar_LevelChange(Sender: TObject);
  75.     // 规拳寒 肺弊 - 包访 窃荐
  76.     // 傍蜡 弃歹 包府 - 包访 窃荐
  77.     procedure ListView_FolderSelectItem(Sender: TObject; Item: TListItem;
  78.       Selected: Boolean);
  79.     
  80.     procedure Btn_FolderReloadClick(Sender: TObject);
  81.     // 器飘 胶牡 - 包访 窃荐
  82.     procedure ListView_FWRuleEnter(Sender: TObject);
  83.     procedure Path_ListEnter(Sender: TObject);
  84.     procedure ListView_FWRuleSelectItem(Sender: TObject; Item: TListItem;
  85.       Selected: Boolean);
  86.     procedure mnuAddIPRuleClick(Sender: TObject);
  87.     procedure mnuAddPathRuleClick(Sender: TObject);
  88.     procedure mnuDirectionClick(Sender: TObject);
  89.     procedure mnuApplicationPathClick(Sender: TObject);
  90.     procedure mnuBytesReceivedClick(Sender: TObject);
  91.     procedure mnuBytesSentClick(Sender: TObject);
  92.     procedure mnuSocketNumberClick(Sender: TObject);
  93.     procedure mnuPermissionClick(Sender: TObject);
  94.     procedure ListView_FWLogEnter(Sender: TObject);
  95.     procedure mnuClearLogClick(Sender: TObject);
  96.     procedure mnuHostNameClick(Sender: TObject);
  97.     procedure BmBtnAddNewClick(Sender: TObject);
  98.     procedure BmtBtnDeleteClick(Sender: TObject);
  99.     procedure BmBtnApplySecurityClick(Sender: TObject);
  100.     procedure BmBtnCancelSecuiryClick(Sender: TObject);
  101.     procedure BmBtnFolderViewClick(Sender: TObject);
  102.     procedure BmBtnFolderInfoClick(Sender: TObject);
  103.     procedure BmBtnFolderStopClick(Sender: TObject);
  104.     procedure BmBtnReloadClick(Sender: TObject);
  105.     procedure BmBtnPortDefaultClick(Sender: TObject);
  106.     procedure BmBtnPortBackDoorClick(Sender: TObject);
  107.     procedure BmBtnPortAllClick(Sender: TObject);
  108.     procedure mnuRuleAddnewClick(Sender: TObject);
  109.     procedure DeleteClick(Sender: TObject);
  110.     procedure mnuRuleChangePermisionClick(Sender: TObject);
  111.   
  112.     { Private declarations }
  113.   public
  114.      FDriverLoaded: BOOL;
  115.      bPortScanning: Boolean;
  116.      ServerSocket:TServerSocket;
  117.      //DDT
  118.      procedure TrapMSG(var MSGX: TMessage);
  119.      //DDT
  120.  {
  121.      Procedure FirewallCallback(name : pchar;
  122.                                 messageBuf : pointer; messageLen : dword;
  123.                                 answerBuf  : pointer    ; answerLen  : dword); stdcall;
  124.   }
  125.   end;
  126.   //DDT
  127.   LogInf=record
  128.      mTime: string[30];
  129.      mDirection:string[10];
  130.      mPermit:String[10];
  131.      mIP:string;
  132.      mHostName:shortstring;
  133.      mPort:String[10];
  134.      mPath:shortstring;
  135.      mToltalRec:String[30];
  136.      mToltalSen:String[30];
  137.      mTotalRecSen:String[30];
  138.      mSockNo:String[20];
  139.   end;
  140.   //DDT
  141. const
  142.   MSGUDP  = 'UDP  / RemoteIP: %3d.%3d.%3d.%3d / LocalPort: %5d';
  143.   MSGTCP  = 'TCP  / RemoteIP: %3d.%3d.%3d.%3d / LocalPort: %5d';
  144.   MSGICMP = 'ICMP / RemoteIP: %3d.%3d.%3d.%3d / Type     : %5d / Code: %5d';
  145.   //DDT
  146.   LogBuffSize=1024;
  147.   //DDT
  148. var
  149.   Form_Option: TForm_Option;
  150.   OS: TOSVersionInfo;
  151.   ChosenRuleType: Integer;
  152.   isLogStarted: Boolean;
  153.   //DDT
  154.   Logs: array[1..LogBuffSize] of LogInf ;
  155.   LogID: Integer;
  156.   mainHWND: HWND;
  157.   //DDT
  158.   //update on march 17, imagelist for logging.
  159.   logImages: TImageList;
  160.   logPaths:TStringlist;
  161.   const PATH_RULE_TYPE: Integer =1;
  162.   const IP_RULE_TYPE: Integer=0;
  163.   const REQUEST_TIMEOUT:Integer=1000;
  164.   
  165. implementation
  166. uses iSafer_Main, iSafer_Resource, iSafer_FWRule,
  167.   PSMFWRule, PSMFWLog,madCodeHook, FWDebug;//iShieldUnit_95, iShieldUnit_NT
  168. {$R *.dfm}
  169. function NameFromIP(ip:string;var hostname:shortstring): Boolean;
  170. var
  171.      WSAData: TWSAData;
  172.      InetAddr: u_long;
  173.      HostEntPtr: PHostEnt;
  174.      retVal:Boolean;
  175.      len: Integer;
  176. begin
  177.      WSAStartUp( $101, WSAData );
  178.      try
  179.           InetAddr := inet_addr(PChar(ip));
  180.           if InetAddr = SOCKET_ERROR then
  181.                raise Exception.Create( 'Invalid address entered' );
  182.           HostEntPtr := GetHostByAddr( @InetAddr, len, AF_INET );
  183.           if HostEntPtr = NIL then
  184.                raise Exception.Create( 'WinSock error: ' + IntToStr( WSAGetLastError() ) );
  185.           // Insert hostname into list
  186.           hostname := String( HostEntPtr^.h_name );
  187.           retVal:=True;
  188.      except
  189.           on E: Exception do begin
  190.               retVal:=False;
  191.           end;
  192.      end;
  193.      Result:=retVal;
  194. end;
  195. {Implement Log process call back function}//20040226
  196. Procedure PSMFW_Callback(name : pchar;
  197.                                 messageBuf : pointer; messageLen : dword;
  198.                                 answerBuf  : pointer    ; answerLen  : dword); stdcall;
  199. Var
  200.      HMapMutex: THandle;
  201.      LogItems:TPSMFWLog;
  202.      //ListItem: TListItem;
  203.      //strTmp:String;
  204.      //NumberOflogLine: Integer;
  205.      MyLogID:Integer;
  206. begin
  207.     //Check where there is space character
  208.     //at the beginning of the line
  209.     if(String(messageBuf)[1]=' ') then Exit;
  210.     MyLogID:=-1;
  211.     HMapMutex := CreateMutex(nil, false, pchar('PSMFirewallApplication'));
  212.     if HMapMutex <> 0 then begin
  213.       if WaitForSingleObject(HMapMutex,100) = WAIT_OBJECT_0 then  begin
  214.         LogID:=(LogID mod LogBuffSize) + 1;
  215.         MyLogID:=LogID;
  216.       end;
  217.       ReleaseMutex(HMapMutex);
  218.       CloseHandle(HMapMutex);
  219.     end;
  220.     if MyLogID>=0 then
  221.     with LogItems do begin
  222.       LogItems:= TPSMFWLog.Create;
  223.       LogItems.AssignLogItems(messageBuf);
  224.       Logs[MyLogID].mTime:=mTime;
  225.       Logs[MyLogID].mDirection:=mDirection;
  226.       Logs[MyLogID].mPermit:=mPermit;
  227.       Logs[MyLogID].mIP:=mIP;
  228.       Logs[MyLogID].mHostName:=mHostName;
  229.       Logs[MyLogID].mPort:=mPort;
  230.       Logs[MyLogID].mPath:=mPath;
  231.       Logs[MyLogID].mToltalRec:=mToltalRec;
  232.       Logs[MyLogID].mToltalSen:=mToltalSen;
  233.       Logs[MyLogID].mTotalRecSen:=mTotalRecSen;
  234.       Logs[MyLogID].mSockNo:=mSockNo;
  235.       Free;
  236.       PostMessage(mainHWND,WM_USER,MyLogID,MyLogID);
  237.     end;//with LogItems do begin
  238. end;
  239. //DDT20040226
  240. procedure TForm_Option.TrapMSG(var MSGX: TMessage);
  241. var
  242.   ListItem: TListItem;
  243. begin
  244.   case MSGX.Msg of
  245.     WM_USER://New log arrived
  246.       begin
  247.         if Form_Option.ListView_FWLog.Items.Count>500 then
  248.         begin
  249.           mnuClearLogClick(Nil);
  250.         end;
  251.         Form_Option.ListView_FWLog.Items.BeginUpdate;
  252.         ListItem:=Form_Option.ListView_FWLog.Items.Add;
  253.         ListItem.ImageIndex:=6;
  254.         //display log information
  255.         with Logs[MSGX.WParam] do
  256.         begin
  257.           //if(not NameFromIP(mIP,mHostName)) then mHostName:= mIP;
  258.           if(logPaths.IndexOf(mPath)<0) then //This Path is not added
  259.           begin
  260.               ListItem.ImageIndex:=logImages.AddIcon(Form_FWRule.GetICON(mPath));
  261.               logPaths.Add(LowerCase(mPath));
  262.           end
  263.           else //path is existing
  264.           begin
  265.                ListItem.ImageIndex:=logPaths.IndexOf(LowerCase(mPath));
  266.           end;
  267.          
  268.           ListItem.Caption:=mTime;
  269.           ListItem.SubItems.Add(mDirection);
  270.           //if  mPermit=0 then  strTmp:='DENY'
  271.           //else  strTmp:='ALLOW';
  272.           ListItem.SubItems.Add(mPermit);
  273.           ListItem.SubItems.Add(mIP);
  274.           ListItem.SubItems.Add(mHostName);
  275.           ListItem.SubItems.Add(mPort);
  276.           ListItem.SubItems.Add(mPath);
  277.           ListItem.SubItems.Add(mToltalRec);
  278.           ListItem.SubItems.Add(mToltalSen);
  279.           ListItem.SubItems.Add(mTotalRecSen);
  280.           ListItem.SubItems.Add(mSockNo);
  281.         end;
  282.         Form_Option.ListView_FWLog.Items.EndUpdate;
  283.         if((Form_Option.ListView_FWLog.Selected=Nil)) then
  284.           ListItem.MakeVisible(TRue);
  285.       end;
  286.     WM_USER+1://Reserved
  287.     else WndProc(MSGX);
  288.   end;
  289. end;
  290. //DDT
  291. // 傍蜡 弃歹 沥焊 舅酒郴扁
  292. procedure TForm_Option.UpdateFolderInfo(Sender: TObject);
  293. var
  294.      i, j: Integer;
  295.      sTemp: TStrings;
  296.      valueName, valuePath, valueRemark, strTemp: String;
  297.      valueSize: Integer;
  298.      valueBuf: PChar;
  299.      Registry, Registry2: TRegistry;
  300.      ListItem: TListItem;
  301. begin
  302.      ListView_Folder.Items.Clear;
  303.      // Windows NT/2000 拌凯
  304.      Registry:= TRegistry.Create;
  305.      Registry.RootKey:= HKEY_LOCAL_MACHINE;
  306.      if Registry.OpenKey('SYSTEMCurrentControlSetServiceslanmanserverShares', False) then begin
  307.           sTemp:= TStringList.Create;
  308.           Registry.GetValueNames(sTemp);
  309.           for i:=0 to sTemp.Count-1 do begin
  310.                valueName:= sTemp.Strings[i];
  311.                valueSize:= Registry.GetDataSize(valueName);
  312.                GetMem(ValueBuf, ValueSize);
  313.                try
  314.                     if Registry.ReadBinaryData(ValueName, ValueBuf^, ValueSize) <> 0 then begin
  315.                          for j:=0 to valueSize-1 do begin
  316.                               if valueBuf[j] = #0 then valueBuf[j]:= '|';
  317.                          end;
  318.                          valuePath:= '';
  319.                          strTemp:= valueBuf;
  320.                          if Pos('|Path=', strTemp) > 0 then begin
  321.                               Delete(strTemp, 1, Pos('|Path=', strTemp) + Length('|Path=') - 1);
  322.                               valuePath:= Copy(strTemp, 1, Pos('|', strTemp) - 1);
  323.                          end;
  324.                          valueRemark:= '';
  325.                          strTemp:= valueBuf;
  326.                          if Pos('|Remark=', strTemp) > 0 then begin
  327.                               Delete(strTemp, 1, Pos('|Remark=', strTemp) + Length('|Remark=') - 1);
  328.                               valueRemark:= Copy(strTemp, 1, Pos('|', strTemp) - 1);
  329.                          end;
  330.                          ListItem:= ListView_Folder.Items.Add;
  331.                          ListItem.ImageIndex:=8;
  332.                          ListItem.Caption:= valueName;
  333.                          ListItem.SubItems.Add(valuePath);
  334.                          ListItem.SubItems.Add(valueRemark);
  335.                     end;
  336.                finally
  337.                     FreeMem(ValueBuf);
  338.                end;
  339.           end;
  340.           sTemp.Free;
  341.           Registry.CloseKey;
  342.      end;
  343.      Registry.Free;
  344.      // Windows 95/98 拌凯
  345.      Registry:= TRegistry.Create;
  346.      Registry.RootKey:= HKEY_LOCAL_MACHINE;
  347.      if Registry.OpenKey('SoftwareMicrosoftWindowsCurrentVersionNetworkLanMan', False) then begin
  348.           sTemp:= TStringList.Create;
  349.           Registry.GetKeyNames(sTemp);
  350.           for i:=0 to sTemp.Count-1 do begin
  351.                valueName:= sTemp.Strings[i];
  352.                Registry2:= TRegistry.Create;
  353.                Registry2.RootKey:= HKEY_LOCAL_MACHINE;
  354.                if Registry2.OpenKey('SoftwareMicrosoftWindowsCurrentVersionNetworkLanMan' + valueName, False) then begin
  355.                     valuePath:= Registry2.ReadString('Path');
  356.                     valueRemark:= Registry2.ReadString('Remark');
  357.                     ListItem:= ListView_Folder.Items.Add;
  358.                     ListItem.Caption:= valueName;
  359.                     ListItem.SubItems.Add(valuePath);
  360.                     ListItem.SubItems.Add(valueRemark);
  361.                     Registry2.CloseKey;
  362.                end;
  363.                Registry2.Free;
  364.           end;
  365.           sTemp.Free;
  366.           Registry.CloseKey;
  367.      end;
  368.      Registry.Free;
  369. end;
  370. procedure TForm_Option.FormCreate(Sender: TObject);
  371. //var
  372. //     bCheck: Boolean;
  373. begin
  374.     //DDT
  375.     LogID:=0;
  376.     mainHWND:=self.Handle;
  377.     WindowProc:=TrapMSG;//Change the WindowProc to User's proc.
  378.     //DDT
  379.      // 函荐 檬扁汲沥
  380.      FDriverLoaded:= False;
  381.      bPortScanning:= False;
  382.      // 滚瓢 劝己拳 惑怕 檬扁拳
  383.      BmBtnFolderView.Enabled:= False;
  384.      BmBtnFolderInfo.Enabled:= False;
  385.      BmBtnFolderStop.Enabled:= False;
  386.      // 扁夯 其捞瘤 汲沥
  387.      PageControl1.ActivePage:= TabSheet_FWRule;
  388.      // OS 滚傈 沥焊 舅酒郴扁
  389.      ZeroMemory(@OS,SizeOf(OS));
  390.      OS.dwOSVersionInfoSize:=SizeOf(OS);
  391.      GetVersionEx(OS);
  392.      // 鸥捞赣 檬扁拳
  393.      //Timer_Init.Enabled:= True;
  394.      BmBtnApplySecurity.Enabled:=False;
  395.      isLogStarted:=False;
  396.      //Init log-image list.
  397.      logImages:=TImageList.Create(Self);
  398.      logPaths:=TStringList.Create;
  399.      logImages.AddIcon(NIl);
  400.      logPaths.Add('%$default$%');
  401.      ListView_FWLog.SmallImages:= logImages;
  402.      ServerSocket:=TServerSocket.Create(Form_Option);
  403. end;
  404. {
  405. Initial
  406. +Read rule set from Registry and Display it
  407. Feb 12, 2004
  408. LuuTruongHuy
  409. }
  410. procedure TForm_Option.FormShow(Sender: TObject);
  411. var
  412.      fwRule:TPSMFWRule;
  413.      ruleList:TStringList;
  414.      ListItem: TListItem;
  415.      i: Integer;
  416.      //for IP rules
  417.      fromIP: string;
  418.      toIP: string;
  419.      fromPort: Integer;
  420.      toPort: Integer;
  421.      //for Path rules
  422.      Path: string;
  423.      Permit: Integer;
  424.      bCheck: boolean;
  425.      imIndex:Integer;
  426. begin
  427.   //Start FireWall Log here--huynote
  428.      bCheck:= CreateIpcQueue('PSMFirewall', PSMFW_Callback);
  429.      if(not bCheck)then
  430.         Showmessage('Error when starting logging Firewall!')
  431.      else
  432.          isLogStarted:=True;
  433.   ruleList:=TStringList.Create;
  434.   fwRule.GetIPRuleList(ruleList);
  435.   //For IP Rule
  436.   for i:=0 to ruleList.Count-1 do
  437.   begin
  438.       fwRule.ExtractIPRule(ruleList[i],fromIP,toIP,fromPort,toPort,Permit);
  439.       ListItem:= ListView_FWRule.Items.Add;
  440.       ListItem.ImageIndex:=5;
  441.       ListItem.Caption:= fromIp+' ~'+toIP;
  442.       ListItem.SubItems.Add(Format('%d ~%d',[fromPort,toPort])) ;
  443.       ListItem.SubItems.Add('TCP/UDP');
  444.       if( permit=1) then
  445.           ListItem.SubItems.Add('ALLOW')
  446.       else
  447.           ListItem.SubItems.Add('DENY');
  448.   end;
  449.   //For Path Rule
  450.   ruleList.Clear;
  451.   ImageListForAppPath.Clear;
  452.   fwRule.GetPathRuleList(ruleList);
  453.   for i:=0 to ruleList.Count-1 do
  454.   begin
  455.       fwRule.ExtractPathRule(ruleList[i],path, Permit);
  456.       imIndex:=ImageListForAppPath.AddIcon(Form_FWRule.GetICON(path));
  457.       ListItem:= Path_List.Items.Add;
  458.       ListItem.Caption:= path;
  459.       ListItem.ImageIndex:=  imIndex;
  460.       if permit=1 then
  461.           ListItem.SubItems.Add('ALLOW')
  462.           //ListItem.SubItems.i
  463.       else
  464.           ListItem.SubItems.Add('DENY');
  465.   end;
  466.   ruleList.Free;
  467.   //For security level.
  468.    TrackBar_Level.Position:=2-fwRule.GetSecurityLevel;
  469.    TrackBar_LevelChange(nil);
  470. end;
  471. {close form options}
  472. procedure TForm_Option.FormDestroy(Sender: TObject);
  473. begin
  474.      // 肺弊沤祸 辆丰
  475.      if isLogStarted then begin
  476.           DestroyIpcQueue('PSMFirewall');
  477.           isLogStarted:=False;
  478.      end;
  479.      while bPortScanning do begin
  480.           Sleep(100);
  481.           Application.ProcessMessages;
  482.      end;
  483.      logImages.Free;
  484.      logPaths.Free;
  485. end;
  486. procedure TForm_Option.FormClose(Sender: TObject;
  487.           var Action: TCloseAction);
  488. var
  489.     HMapMutex:THandle;
  490. begin
  491.      //Hide form
  492.      Action:=caHide;   //TRR
  493.      if isLogStarted then begin
  494.           //Stop Logging function.
  495.           DestroyIpcQueue('PSMFirewall');
  496.           isLogStarted:=False;
  497.      end;
  498.      ListView_FWRule.Clear;
  499.      HMapMutex := CreateMutex(nil, false, pchar('PSMFirewallApplication'));
  500.      if HMapMutex <> 0 then begin
  501.           if WaitForSingleObject(HMapMutex,100) = WAIT_OBJECT_0 then
  502.           begin
  503.                 ListView_FWLog.Clear;
  504.           end;
  505.      end;
  506.      ReleaseMutex(HMapMutex);
  507.      CloseHandle(HMapMutex);
  508.      Path_List.Clear;
  509.      while bPortScanning do begin
  510.           Sleep(100);
  511.           Application.ProcessMessages;
  512.      end;
  513. end;
  514. {
  515. Show Shared Folder List View
  516. }
  517. procedure TForm_Option.TabSheet_FolderShow(Sender: TObject);
  518. begin
  519.      UpdateFolderInfo(self);
  520. end;
  521. {
  522. Show tab-page of choosing security level.
  523. }
  524. procedure TForm_Option.TabSheet_LevelShow(Sender: TObject);
  525. begin
  526. ;
  527. end;
  528. procedure TForm_Option.TrackBar_LevelChange(Sender: TObject);
  529. var
  530.      nLevel: Integer;
  531. begin
  532.      nLevel:= 2 - TrackBar_Level.Position;
  533.      if (nLevel = 0) then begin
  534.           Label_Level0.Caption:= 'Low';
  535.           Label_Level2.Caption:= '- All IP/Ports are allow except:';
  536.           Label_Level3.Caption:= '- All dened IP/Ports in the rule set are blocked';
  537.      end else if (nLevel = 1) then begin
  538.           Label_Level0.Caption:= 'Medium';
  539.           Label_Level2.Caption:= '- Allow only Well - Known Ports';
  540.           Label_Level3.Caption:= '- All dened IP/Ports in the rule set are blocked'
  541.      end else if (nLevel = 2) then begin
  542.           Label_Level0.Caption:= 'High';
  543.           Label_Level2.Caption:= '- Block all IP/Ports except:';
  544.           Label_Level3.Caption:= '- All allowed IP/Ports in the rule set are allowed'
  545.      end;
  546.      //Btn_Level_OK.Enabled:= True;
  547.      BmBtnApplySecurity.Enabled:=True;
  548. end;
  549. {
  550. Select Shared foler in list view
  551. }
  552. procedure TForm_Option.ListView_FolderSelectItem(Sender: TObject;
  553.   Item: TListItem; Selected: Boolean);
  554. begin
  555.      if ListView_Folder.ItemIndex < 0 then begin
  556.           BmBtnFolderView.Enabled:= False;
  557.           BmBtnFolderInfo.Enabled:= False;
  558.           BmBtnFolderStop.Enabled:= False;
  559.      end else begin
  560.           BmBtnFolderView.Enabled:= True;
  561.           BmBtnFolderInfo.Enabled:= True;
  562.           BmBtnFolderStop.Enabled:= True;
  563.      end;
  564. end;
  565. {
  566. For shared folder
  567. }
  568. {
  569. procedure TForm_Option.Btn_FolderViewClick(Sender: TObject);     // 弃歹 郴侩 焊扁
  570. var
  571.      nIndex: Integer;
  572.      strPath: String;
  573. begin
  574.      nIndex:= ListView_Folder.ItemIndex;
  575.      if nIndex >= 0 then begin
  576.           strPath:= ListView_Folder.Items.Item[nIndex].SubItems.Strings[0];
  577.           ShellExecute(Handle,'open','explorer',PChar(strPath),'',SW_SHOWNORMAL);
  578.      end;
  579. end;
  580. }
  581. {
  582. For shared folder
  583. }
  584. {
  585. For shared folder
  586. }
  587. procedure TForm_Option.Btn_FolderReloadClick(Sender: TObject);   // 货肺 绊魔
  588. begin
  589.      UpdateFolderInfo(self);
  590. end;
  591. procedure TForm_Option.ListView_FWRuleEnter(Sender: TObject);
  592. begin
  593.     ChosenRuleType:=IP_RULE_TYPE;
  594. end;
  595. procedure TForm_Option.Path_ListEnter(Sender: TObject);
  596. begin
  597.      ChosenRuleType:=PATH_RULE_TYPE;
  598. end;
  599. procedure TForm_Option.ListView_FWRuleSelectItem(Sender: TObject;
  600.   Item: TListItem; Selected: Boolean);
  601. begin
  602. end;
  603. {Add IP Rule from Log list by selecting add IP rule on popup menu}
  604. procedure TForm_Option.mnuAddIPRuleClick(Sender: TObject);
  605. var
  606.     //selectedIndex: Integer;
  607.     //Rule Items
  608.     //time:string;
  609.     ip: string;
  610.     //port: integer;
  611.     sPort:String;
  612.     permission: integer;
  613.     //SelItem:TListItem;
  614.     //strTmp:String;
  615.     fwRule:TPSMFWRule;
  616. begin
  617.      if  ListView_FWLog.Selected= nil then
  618.           MessageBox(Self.Handle,'Select one item in log list first','PSM Firewall',MB_OK or MB_ICONEXCLAMATION)
  619.      else
  620.           begin
  621.                 ip:= ListView_FWLog.Selected.SubItems[2];    //IP column
  622.                 sPort:= ListView_FWLog.Selected.SubItems[4]; //Port column
  623.                 if CompareText('DENY',ListView_FWLog.Selected.SubItems[1])=0 then
  624.                     Permission:=0
  625.                 else Permission:=1;
  626.                 //ShowMessage(ip);
  627.                 //Form_FWRule.MaskEdit_sIP.Text:=fwRule.IPStd(PChar(ip));
  628.                 Form_FWRule.SetIPFrom(fwRule.IPStd(PChar(ip)));
  629.                 Form_FWRule.MaskEdit_sPort.Text:=Format('%5s',[sPort]);
  630.                 Form_FWRule.Radio_IP_Single.Checked:=TRUE;
  631.                 Form_FWRule.Radio_Port_Single.Checked:=TRUE;
  632.                 if Permission=1 then
  633.                     Form_FWRule.Radio_Access_Allow.Checked:=True
  634.                 else
  635.                     Form_FWRule.Radio_Access_Deny.Checked:=True;
  636.                 Form_FWRule.FWRule.ActivePage:=Form_FWRule.IpRulePage;
  637.                 Form_FWRule.Show;
  638.           end;
  639. end;
  640. {Add Path Rule From Log List}
  641. procedure TForm_Option.mnuAddPathRuleClick(Sender: TObject);
  642. var
  643.      path: string;
  644.      permission: integer;
  645. begin
  646.      if  ListView_FWLog.Selected= nil then
  647.           MessageBox(Self.Handle,'Select one item in log list first','PSM Firewall',MB_OK or MB_ICONEXCLAMATION)
  648.      else
  649.           begin
  650.                 path:= ListView_FWLog.Selected.SubItems[5]; //Path column
  651.                 if CompareText('DENY',ListView_FWLog.Selected.SubItems[1])=0 then
  652.                     Permission:=0
  653.                 else Permission:=1;
  654.                 //ShowMessage(path);
  655.                 Form_FWRule.PathEdit.Text:=LowerCase(path);
  656.                 if Permission=1 then
  657.                     Form_FWRule.Permission.ItemIndex:=0
  658.                 else
  659.                     Form_FWRule.Permission.ItemIndex:=1;
  660.                 Form_FWRule.FWRule.ActivePage:=Form_FWRule.PathRulePage;
  661.                 Form_FWRule.Show;
  662.           end;
  663. end;
  664. {display or hide direction column}
  665. procedure TForm_Option.mnuDirectionClick(Sender: TObject);
  666. begin
  667.     if mnuDirection.Checked then
  668.     begin
  669.          mnuDirection.Checked:= False;
  670.          ListView_FWLog.Columns[1].Width:=0;
  671.     end
  672.     else
  673.     begin
  674.          mnuDirection.Checked:= TRue;
  675.          ListView_FWLog.Columns[1].Width:=70;
  676.     end;
  677. end;
  678. {display application path or not}
  679. procedure TForm_Option.mnuApplicationPathClick(Sender: TObject);
  680. begin
  681.     if mnuApplicationpath.Checked then
  682.     begin
  683.          mnuApplicationpath.Checked:= False;
  684.          ListView_FWLog.Columns[6].Width:=0;
  685.     end
  686.     else
  687.     begin
  688.          mnuApplicationpath.Checked:= True;
  689.          ListView_FWLog.Columns[6].Width:=250;
  690.     end;
  691. end;
  692. {display or hide bytes received}
  693. procedure TForm_Option.mnuBytesReceivedClick(Sender: TObject);
  694. begin
  695.     if mnuBytesReceived.Checked then
  696.     begin
  697.          mnuBytesReceived.Checked:= False;
  698.          ListView_FWLog.Columns[7].Width:=0;
  699.     end
  700.     else
  701.     begin
  702.          mnuBytesReceived.Checked:= True;
  703.          ListView_FWLog.Columns[7].Width:=70;
  704.     end;
  705. end;
  706. procedure TForm_Option.mnuBytesSentClick(Sender: TObject);
  707. begin
  708.     if mnuBytesSent.Checked then
  709.     begin
  710.          mnuBytesSent.Checked:= False;
  711.          ListView_FWLog.Columns[8].Width:=0;
  712.     end
  713.     else
  714.     begin
  715.          mnuBytesSent.Checked:= True;
  716.          ListView_FWLog.Columns[8].Width:=70;
  717.     end;
  718. end;
  719. procedure TForm_Option.mnuSocketNumberClick(Sender: TObject);
  720. begin
  721.     if mnuSocketNumber.Checked then
  722.     begin
  723.          mnuSocketNumber.Checked:= False;
  724.          ListView_FWLog.Columns[10].Width:=0;
  725.          //ListView_FWLog.Columns[10].//
  726.     end
  727.     else
  728.     begin
  729.          mnuSocketNumber.Checked:= True;
  730.          ListView_FWLog.Columns[10].Width:=50;
  731.     end;
  732. end;
  733. procedure TForm_Option.mnuPermissionClick(Sender: TObject);
  734. begin
  735.      if mnuPermission.Checked then
  736.     begin
  737.          mnuPermission.Checked:= False;
  738.          ListView_FWLog.Columns[2].Width:=0;
  739.     end
  740.     else
  741.     begin
  742.          mnuPermission.Checked:= True;
  743.          ListView_FWLog.Columns[2].Width:=70;
  744.     end;
  745. end;
  746. procedure TForm_Option.ListView_FWLogEnter(Sender: TObject);
  747. begin
  748.      if(ListView_FWLog.Selected<>Nil) then
  749.           ListView_FWLog.Selected.MakeVisible(False);
  750. end;
  751. procedure TForm_Option.mnuClearLogClick(Sender: TObject);
  752. //var
  753. //HMapMutex: THandle;
  754. begin
  755.      //HMapMutex := CreateMutex(nil, false, pchar('PSMFirewallApplication'));
  756.      //if HMapMutex <> 0 then begin
  757.      //     if WaitForSingleObject(HMapMutex,100) = WAIT_OBJECT_0 then
  758.       //    begin
  759.                ListView_FWLog.Visible:=False;
  760.                ListView_FWLog.Items.BeginUpdate;
  761.                ListView_FWLog.Items.Clear;
  762.                ListView_FWLog.Items.EndUpdate;
  763.                ListView_FWLog.Visible:=True;
  764.         //  end;
  765.      //end;
  766.      //ReleaseMutex(HMapMutex);
  767.      //CloseHandle(HMapMutex);
  768. end;
  769. procedure TForm_Option.mnuHostNameClick(Sender: TObject);
  770. begin
  771.      if mnuHostName.Checked then
  772.     begin
  773.          mnuHostName.Checked:= False;
  774.          ListView_FWLog.Columns[4].Width:=0;
  775.     end
  776.     else
  777.     begin
  778.          mnuHostName.Checked:= True;
  779.          ListView_FWLog.Columns[4].Width:=150;
  780.     end;
  781. end;
  782. {
  783. Add new Rule, This function displays rule editor dialog
  784. }
  785. procedure TForm_Option.BmBtnAddNewClick(Sender: TObject);
  786. begin
  787.       Form_FWRule.ShowModal;
  788. end;
  789. {
  790. remove Rules from display list
  791. Huy marked.
  792. }
  793. procedure TForm_Option.BmtBtnDeleteClick(Sender: TObject);
  794. var
  795.      nIndex: Integer;
  796.      //strFileName: String;
  797.      //bSuccess: Boolean;
  798.      //TempFile: TextFile;
  799.      //Huy
  800.      RuleIdentify: String;
  801.      //StrTmp:String;
  802.      FromIP,ToIP:string;
  803.      FWRule: TPSMFWRule;
  804.      Delim: Integer;
  805.      //h:String;
  806. begin
  807.         nIndex:= ListView_FWRule.ItemIndex;
  808.      if((nIndex >= 0) and (ChosenRuleType=IP_RULE_TYPE)) then begin
  809.           if Application.MessageBox('Delete the selected IP/Port rule?', 'PSM Firewall', MB_YESNO) = IDYES then begin
  810.                {Remove from Registry}
  811.                RuleIdentify:= ListView_FWRule.Items.Item[nIndex].Caption;
  812.                Delim:=Pos('~',RuleIdentify);
  813.                FromIP:=Trim(LeftStr(RuleIdentify,Delim-1));
  814.                ToIP:=Trim(RightStr(RuleIdentify,Length(RuleIdentify)-Delim));
  815.                //AnsiReplaceStr(RuleIdentify,'~','|');
  816.                FWRule.RemoveIPRule(PChar(FromIP),PChar(ToIP));
  817.                {Remore from Displaying List}
  818.                ListView_FWRule.Items.Delete(nIndex);
  819.                if(Form_Main.bFWStart)then
  820.                 ApplyFWStatus(1);
  821.           end;
  822.      end else begin //No rule is selected in IP Rule list, then try with Path List
  823.           nIndex:= Path_List.ItemIndex;
  824.           if(( nIndex>=0) and (ChosenRuleType=PATH_RULE_TYPE)) then
  825.                begin
  826.                     if Application.MessageBox('Delete the selected application rule?', 'PSM Firewall', MB_YESNO) = IDYES then begin
  827.                         //delete from registry
  828.                         RuleIdentify:= Path_List.Items.Item[nIndex].Caption;
  829.                         FWRule.RemovePathRule(Pchar(RuleIdentify));
  830.                         //delre from List
  831.                         Path_List.Items.Delete(nIndex);
  832.                         if(Form_Main.bFWStart)then
  833.                               ApplyFWStatus(1);
  834.                     end;
  835.                end
  836.           else
  837.            Application.MessageBox('Select the rule first!', 'iSafer', MB_OK);
  838.      end;
  839. end;
  840. {
  841. Apply sercurity level
  842. }
  843. procedure TForm_Option.BmBtnApplySecurityClick(Sender: TObject);
  844. var
  845.      nLevel: Integer;
  846.      fwRuler:TPSMFWRule;
  847. begin
  848.      nLevel:= 2 - TrackBar_Level.Position;
  849.      fwRuler.SetSecurityLevel(nLevel);
  850.      BmBtnApplySecurity.Enabled:= False;
  851.      if(Form_Main.bFWStart)then
  852.               ApplyFWStatus(1);
  853.               
  854. end;
  855. {
  856. Cancel apply sercurity level
  857. }
  858. procedure TForm_Option.BmBtnCancelSecuiryClick(Sender: TObject);
  859. var
  860.      nLevel: Integer;
  861.      fwRule:TPSMFWRule;
  862. begin
  863.      nLevel:=fwRule.GetSecurityLevel;
  864.      TrackBar_Level.Position:= 2 - nLevel;
  865.      BmBtnApplySecurity.Enabled:= False;
  866. end;
  867. procedure TForm_Option.BmBtnFolderViewClick(Sender: TObject);
  868. var
  869.      nIndex: Integer;
  870.      strPath: String;
  871. begin
  872.      nIndex:= ListView_Folder.ItemIndex;
  873.      if nIndex >= 0 then begin
  874.           strPath:= ListView_Folder.Items.Item[nIndex].SubItems.Strings[0];
  875.           ShellExecute(Handle,'open','explorer',PChar(strPath),'',SW_SHOWNORMAL);
  876.      end;
  877. end;
  878. procedure TForm_Option.BmBtnFolderInfoClick(Sender: TObject);
  879. var
  880.      nIndex: Integer;
  881.      strItem: String;
  882.      ExecInfo: TShellExecuteInfo;
  883. begin
  884.      nIndex:= ListView_Folder.ItemIndex;
  885.      if nIndex >= 0 then begin
  886.           strItem:= ListView_Folder.Items.Item[nIndex].SubItems.Strings[0];
  887.           FillChar(ExecInfo, SizeOf(ExecInfo), 0);
  888.           ExecInfo.cbSize := SizeOf(TShellExecuteInfo);
  889.           ExecInfo.fMask := SEE_MASK_INVOKEIDLIST or SEE_MASK_FLAG_NO_UI or SEE_MASK_NOCLOSEPROCESS;
  890.           ExecInfo.lpFile := PChar(strItem);
  891.           ExecInfo.lpVerb := 'Properties';
  892.           ExecInfo.nShow := SW_SHOWDEFAULT;
  893.           ShellExecuteEx(@ExecInfo);
  894.      end;
  895. end;
  896. procedure TForm_Option.BmBtnFolderStopClick(Sender: TObject);
  897.    type
  898.      tFuncNT = function(ServerName, NetName: LPWSTR; Reserved: DWORD): DWord; stdcall;
  899.      tFunc95 = function(ServerName, NetName: PChar; Reserved: DWORD): Integer; stdcall;
  900. var
  901.      nIndex: Integer;
  902.      strNameNT: WideString;
  903.      strName95: String;
  904.      strPath, strTemp: String;
  905.      DLLHandle: THandle;
  906.      MyFuncNT: TFuncNT;
  907.      MyFunc95: TFunc95;
  908. begin
  909.      nIndex:= ListView_Folder.ItemIndex;
  910.      if nIndex >= 0 then begin
  911.           strNameNT:= ListView_Folder.Items.Item[nIndex].Caption;
  912.           strName95:= ListView_Folder.Items.Item[nIndex].Caption;
  913.           strPath:= ListView_Folder.Items.Item[nIndex].SubItems.Strings[0];
  914.           strTemp:= strName95 + ' -- Unshare the selected folder?';
  915.           if Application.MessageBox(PChar(strTemp), 'iSafer', MB_YESNO) = IDYES then begin
  916.                if OS.dwPlatformId = VER_PLATFORM_WIN32_NT then begin
  917.                     // Windows NT/2000 拌凯
  918.                     DLLHandle:= LoadLibrary('netapi32.dll');
  919.                     if DLLHandle < 32 then exit;
  920.                     @MyFuncNT:= GetProcAddress(DLLHandle, 'NetShareDel');
  921.                     if not (@MyFuncNT = nil) then begin
  922.                          MyFuncNT('', PWideChar(strNameNT), 0);
  923.                     end;
  924.                     FreeLibrary(DLLHandle);
  925.                end else begin
  926.                     // Windows 95/98 拌凯
  927.                     DLLHandle:= LoadLibrary('svrapi.dll');
  928.                     if DLLHandle < 32 then exit;
  929.                     @MyFunc95:= GetProcAddress(DLLHandle, 'NetShareDel');
  930.                     if not (@MyFunc95 = nil) then begin
  931.                          MyFunc95('', PChar(strName95), 0);
  932.                     end;
  933.                     FreeLibrary(DLLHandle);
  934.                end;
  935.                UpdateFolderInfo(self);   // 傍蜡 弃歹 沥焊 舅酒郴扁
  936.           end;
  937.      end;
  938. end;
  939. procedure TForm_Option.BmBtnReloadClick(Sender: TObject);
  940. begin
  941.         UpdateFolderInfo(self);
  942. end;
  943. procedure TForm_Option.BmBtnPortDefaultClick(Sender: TObject);
  944. var
  945.      i, nPortCount: Integer;
  946.      PortNumber:Integer;
  947.      currTh:Integer;
  948.      ListItem: TListItem;
  949. begin
  950.       nPortCount:=0;
  951.       ListView_PortTemp.Width:= 0;
  952.       ListView_PortTemp.Height:= 0;
  953.       ListView_PortTemp.Visible:= True;
  954.       for i:=0 to ListView_PortTemp.Items.Count-1 do
  955.                if ListView_PortTemp.Items.Item[i].SubItems.Strings[1] = 'Default' then begin
  956.                     inc(nPortCount);
  957.                end;
  958.       ProgressBar.Min:=0;
  959.       ProgressBar.Max:=100;
  960.       ProgressBar.Position:=0;
  961.       bPortScanning:= True;
  962.       ListView_Port.Items.BeginUpdate;
  963.       ListView_Port.Items.Clear;
  964.       currTh:=0;
  965.       for i:=0 to ListView_PortTemp.Items.Count-1 do begin
  966.                if ListView_PortTemp.Items.Item[i].SubItems.Strings[1] = 'Default' then
  967.                begin
  968.                     ProgressBar.Position := Round(100*currTh/nPortCount);
  969.                     PortNumber:= StrToInt(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
  970.                     try
  971.                          // Trying to open serversocket
  972.                          ServerSocket.Port := PortNumber;
  973.                          ServerSocket.Open();
  974.                          ServerSocket.Close();
  975.                          ListItem:= ListView_Port.Items.Add;
  976.                          ListItem.ImageIndex:=7;
  977.                          ListItem.Caption:= ListView_PortTemp.Items.Item[i].Caption;
  978.                          ListItem.SubItems.Add(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
  979.                          ListItem.SubItems.Add('Close');
  980.                     except
  981.                          ListItem:= ListView_Port.Items.Add;
  982.                          ListItem.ImageIndex:=7;
  983.                          ListItem.Caption:= ListView_PortTemp.Items.Item[i].Caption;
  984.                          ListItem.SubItems.Add(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
  985.                          ListItem.SubItems.Add(' Open ');
  986.                     end; // try/except
  987.                     inc(currTh);
  988.                end;
  989.           end;
  990.       bPortScanning:= False;
  991.       ListView_Port.Items.EndUpdate;
  992.       ListView_PortTemp.Visible:= False;
  993.       ProgressBar.Position :=0;
  994. end;
  995. procedure TForm_Option.BmBtnPortBackDoorClick(Sender: TObject);
  996. var
  997.      i, nPortCount: Integer;
  998.      PortNumber:Integer;
  999.      currTh:Integer;
  1000.      ListItem: TListItem;
  1001. begin
  1002.      //Count the number of backdoor port
  1003.       nPortCount:=0;
  1004.       ListView_PortTemp.Width:= 0;
  1005.       ListView_PortTemp.Height:= 0;
  1006.       ListView_PortTemp.Visible:= True;
  1007.       for i:=0 to ListView_PortTemp.Items.Count-1 do
  1008.                if ListView_PortTemp.Items.Item[i].SubItems.Strings[1] = 'Backdoor' then begin
  1009.                     inc(nPortCount);
  1010.                end;
  1011.       ProgressBar.Min:=0;
  1012.       ProgressBar.Max:=100;
  1013.       ProgressBar.Position:=0;
  1014.       bPortScanning:= True;
  1015.       ListView_Port.Items.BeginUpdate;
  1016.       ListView_Port.Items.Clear;
  1017.       currTh:=0;
  1018.       for i:=0 to ListView_PortTemp.Items.Count-1 do begin
  1019.                if ListView_PortTemp.Items.Item[i].SubItems.Strings[1] = 'Backdoor' then
  1020.                begin
  1021.                     ProgressBar.Position := Round(100*currTh/nPortCount);
  1022.                     PortNumber:= StrToInt(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
  1023.                     try
  1024.                          // Trying to open serversocket
  1025.                          ServerSocket.Port := PortNumber;
  1026.                          ServerSocket.Open();
  1027.                          ServerSocket.Close();
  1028.                          ListItem:= ListView_Port.Items.Add;
  1029.                          ListItem.ImageIndex:=7;
  1030.                          ListItem.Caption:= ListView_PortTemp.Items.Item[i].Caption;
  1031.                          ListItem.SubItems.Add(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
  1032.                          ListItem.SubItems.Add('Close');
  1033.                     except
  1034.                          ListItem:= ListView_Port.Items.Add;
  1035.                          ListItem.ImageIndex:=7;
  1036.                          ListItem.Caption:= ListView_PortTemp.Items.Item[i].Caption;
  1037.                          ListItem.SubItems.Add(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
  1038.                          ListItem.SubItems.Add(' Open ');
  1039.                     end; // try/except
  1040.                     inc(currTh);
  1041.                end;
  1042.           end;
  1043.       bPortScanning:= False;
  1044.       ListView_Port.Items.EndUpdate;
  1045.       ListView_PortTemp.Visible:= False;
  1046.       ProgressBar.Position :=0;
  1047. end;
  1048. procedure TForm_Option.BmBtnPortAllClick(Sender: TObject);
  1049. var
  1050.      i, nPortCount: Integer;
  1051.      PortNumber:Integer;
  1052.      currTh:Integer;
  1053.      ListItem: TListItem;
  1054. begin
  1055.       nPortCount:=0;
  1056.       ListView_PortTemp.Width:= 0;
  1057.       ListView_PortTemp.Height:= 0;
  1058.       ListView_PortTemp.Visible:= True;
  1059.       for i:=0 to ListView_PortTemp.Items.Count-1 do
  1060.            inc(nPortCount);
  1061.       ProgressBar.Min:=0;
  1062.       ProgressBar.Max:=100;
  1063.       ProgressBar.Position:=0;
  1064.       bPortScanning:= True;
  1065.       ListView_Port.Items.BeginUpdate;
  1066.       ListView_Port.Items.Clear;
  1067.       currTh:=0;
  1068.       for i:=0 to ListView_PortTemp.Items.Count-1 do begin
  1069.                //if ListView_PortTemp.Items.Item[i].SubItems.Strings[1] = 'Backdoor' then
  1070.                     ProgressBar.Position := Round(100*currTh/nPortCount);
  1071.                     PortNumber:= StrToInt(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
  1072.                          // Trying to open serversocket
  1073.                     try
  1074.                            ServerSocket.Port := PortNumber;
  1075.                            ServerSocket.Open();
  1076.                            ServerSocket.Close();
  1077.                            ListItem:= ListView_Port.Items.Add;
  1078.                            ListItem.ImageIndex:=7;
  1079.                            ListItem.Caption:= ListView_PortTemp.Items.Item[i].Caption;
  1080.                            ListItem.SubItems.Add(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
  1081.                            ListItem.SubItems.Add('Close');
  1082.                     except
  1083.                            ListItem:= ListView_Port.Items.Add;
  1084.                            ListItem.ImageIndex:=7;
  1085.                            ListItem.Caption:= ListView_PortTemp.Items.Item[i].Caption;
  1086.                            ListItem.SubItems.Add(ListView_PortTemp.Items.Item[i].SubItems.Strings[0]);
  1087.                            ListItem.SubItems.Add(' Open ');
  1088.                     end;//Try
  1089.                     inc(currTh);
  1090.                //end;
  1091.           end;
  1092.       bPortScanning:= False;
  1093.       ListView_Port.Items.EndUpdate;
  1094.       ListView_PortTemp.Visible:= False;
  1095.       ProgressBar.Position :=0;
  1096. end;
  1097. procedure TForm_Option.mnuRuleAddnewClick(Sender: TObject);
  1098. begin
  1099.       if(ChosenRuleType=IP_RULE_TYPE) then
  1100.         begin
  1101.             Form_FWRule.FWRule.ActivePage:=Form_FWRule.IpRulePage;
  1102.             Form_FWRule.ShowModal;
  1103.         end
  1104.       else  if (ChosenRuleType=PATH_RULE_TYPE) then
  1105.         begin
  1106.                 Form_FWRule.FWRule.ActivePage:=Form_FWRule.PathRulePage;
  1107.                 Form_FWRule.ShowModal;
  1108.         end;
  1109. end;
  1110. procedure TForm_Option.DeleteClick(Sender: TObject);
  1111. begin
  1112.         BmtBtnDeleteClick(nil);
  1113. end;
  1114. procedure TForm_Option.mnuRuleChangePermisionClick(Sender: TObject);
  1115. var
  1116.     FWRule :TPSMFWRule;
  1117.     RuleIdentify:string;
  1118.     nIndex:Integer;
  1119.     Delim:Integer;
  1120.     FromIP:string;
  1121.     ToIP:string;
  1122.     FromPort:string;
  1123.     ToPort:string;
  1124.     PortRange:string;
  1125.     SlectedItem:TListItem;
  1126.     Permision:String;
  1127.     PermissionValue:Integer;
  1128.     strmsg:string;
  1129. begin
  1130.      if(ChosenRuleType=IP_RULE_TYPE) then
  1131.            begin
  1132.                nIndex:=  ListView_FWRule.ItemIndex;
  1133.                if(nIndex>=0) then
  1134.                begin
  1135.                       SlectedItem:= ListView_FWRule.Items.Item[nIndex];
  1136.                       RuleIdentify:= SlectedItem.Caption;
  1137.                       Delim:=Pos('~',RuleIdentify);
  1138.                       FromIP:=Trim(LeftStr(RuleIdentify,Delim-1));
  1139.                       ToIP:=Trim(RightStr(RuleIdentify,Length(RuleIdentify)-Delim));
  1140.                       PortRange:= SlectedItem.SubItems[0];
  1141.                       Permision:= SlectedItem.SubItems[2];
  1142.                       Delim:=Pos('~',PortRange);
  1143.                       FromPort:=Trim(LeftStr(PortRange,Delim-1));
  1144.                       ToPort:=Trim(RightStr(PortRange,Length(PortRange)-Delim));
  1145.                       if(Permision='DENY') then PermissionValue:=0
  1146.                         else  PermissionValue:=1;
  1147.                       strmsg:='IP Rule : '#13#10'IP range:   [From '+ FromIP+' , To '+ ToIP+']'#13#10;
  1148.                       strmsg:=strmsg+'Port range:   ['+FromPort+', '+ ToPort+']'#13#10;
  1149.                       strmsg:=strmsg+'Permission:   '+ Permision+''#13#10;
  1150.                       strmsg:=strmsg+'**********************'#13#10;
  1151.                       if(Permision='DENY') then
  1152.                            strmsg:=strmsg+'Change permission to (Allow)?'
  1153.                       else  strmsg:=strmsg+'Change permission to (Deny)?';
  1154.                       if Application.MessageBox(PChar(strmsg),'PSM Firewall', MB_YESNO) = IDYES then
  1155.                         begin
  1156.                             if(Permision='DENY') then
  1157.                                 begin
  1158.                                         FWRule.ModifyIPrule(Pchar(fromIP),PChar(toIP),StrToInt(fromPort),strtoint(toPort),StrToInt(fromPort),strtoint(toPort),1);
  1159.                                         SlectedItem.SubItems[2]:='ALLOW';
  1160.                                 end
  1161.                             else
  1162.                                 begin
  1163.                                         FWRule.ModifyIPrule(Pchar(fromIP),PChar(toIP),StrToInt(fromPort),strtoint(toPort),strtoint(fromPort),strtoint(toPort),0) ;
  1164.                                         SlectedItem.SubItems[2]:='DENY' ;
  1165.                                 end;
  1166.                             if(Form_Main.bFWStart) then
  1167.                                 ApplyFWStatus(1);
  1168.                         end
  1169.                         //   Application.MessageBox('No rule is selected', 'PSM Firewall', MB_OK);
  1170.                end;
  1171.            end
  1172.         else if(ChosenRuleType=PATH_RULE_TYPE) then
  1173.            begin
  1174.                 nIndex:=  Path_List.ItemIndex;
  1175.                 if(nIndex>=0) then
  1176.                     begin
  1177.                          SlectedItem:=Path_List.Items.Item[nIndex];
  1178.                          RuleIdentify:= SlectedItem.Caption;
  1179.                          Permision:= SlectedItem.SubItems[0];
  1180.                          strmsg:='Application rule : '#13#10'program path =   '+RuleIdentify+' '#13#10;
  1181.                          strmsg:=strmsg+'Permission:   '+ Permision+''#13#10;
  1182.                          strmsg:=strmsg+'**********************'#13#10;
  1183.                          if(Permision='DENY') then
  1184.                            strmsg:=strmsg+'Change permission to (Allow)?'
  1185.                          else  strmsg:=strmsg+'Change permission to (Deny)?';
  1186.                         if Application.MessageBox(PChar(strmsg),'PSM Firewall', MB_YESNO) = IDYES then
  1187.                              begin
  1188.                                    if(Permision='DENY') then
  1189.                                          begin
  1190.                                              FWRule.ModifyPathrule(Pchar(RuleIdentify),1) ;
  1191.                                              SlectedItem.SubItems[0]:='ALLOW';
  1192.                                           end
  1193.                                    else
  1194.                                          begin
  1195.                                             FWRule.ModifyPathrule(Pchar(RuleIdentify),0) ;
  1196.                                             SlectedItem.SubItems[0]:='DENY' ;
  1197.                                           end;
  1198.                                     if(Form_Main.bFWStart) then
  1199.                                              ApplyFWStatus(1);
  1200.                              end;
  1201.                     end
  1202.                 else
  1203.                         begin
  1204.                               Application.MessageBox('No rule is selected', 'PSM Firewall', MB_OK);
  1205.                         end;
  1206.            end;
  1207. end;
  1208. end.