#!/usr/local/bin/perl -w # # m_search.cgi # # meta search engine in CGI using htdig # Usage: m_search.cgi [remote_server [port_number [cf_file]]] # ÆÐÅ°Áö »ç¿ë ¼±¾ð require 5.002; use Socket; # ÃʱⰪ ¼³Á¤, cf´Â configuration ÆÄÀÏÀÇ À̸§, localhost´Â htdigÀ» ¼³Ä¡ÇÑ # ½Ã½ºÅÛÀÇ À̸§(hostname command¸¦ ÀÌ¿ëÇÔ) $cf = 'm_search.cf'; $localhost = `hostname`; chomp $localhost; # HTML Çì´õ Ãâ·Â(¼­ºê·çƾ È£Ãâ) &print_head; # CGIÀÇ ¿äû ÇüÅ °áÁ¤(GET/POST) if ($ENV{'REQUEST_METHOD'} eq 'GET') { (@pairs) = split(/&/, $ENV{'QUERY_STRING'}); } else { while (<>) { (@pairs) = split(/&/, $_); } } # CGIÀÇ ÀÔ·Â(@pairs)À¸·ÎºÎÅÍ name:value¸¦ hashÇü º¯¼ö FORM¿¡ ÀúÀå foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); chomp $name; chomp $value; $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/~!/ ~!/g; $FORM{$name} = $value; #print "$name=$value
\n"; } # CGIÀÔ·Â Áß, °Ë»ö¾î¿Í °Ë»ö¿äû ¼­¹ö¸í, max hop¼ö¸¦ ±¸ÇÑ´Ù. $keyword = $FORM{'keyword'}; $requester = $FORM{'requester'}; $maxhop = $FORM{'maxhop'}; # °Ë»ö¾î°¡ ÀԷµÇÁö ¾ÊÀº °æ¿ì ó¸® if ($keyword eq "") { print "¾î¶² °Ë»ö¾îµµ ÀԷµÇÁö ¾Ê¾Ò½À´Ï´Ù. ´Ù½Ã ½ÃÀÛÇϽʽÿÀ.
\n"; &print_form(""); &print_tail; exit(0); } # configuration ÆÄÀÏÀ» ÀÐ¾î ¼³Á¤À» ¸ÂÃá´Ù. open(CONF, $cf) or print "can't open $cf\n"; while () { if (/^nodename:\s*(\S+)/) { $server = $1; } elsif (/^domain:\s*(\S+)/) { $domain = $1; } elsif (/^port:\s*(\d+)/) { $port = $1; } elsif (/^parents:\s*(.*)/) { $parents = $1; } elsif (/^children:\s*(.*)/) { $children = $1; } } # ¼³Á¤ÀÌ ½ÇÆÐÇßÀ» °æ¿ì ±âº»°ªÀ¸·Î ÁöÁ¤ÇÑ´Ù. $localhost .= ".$domain"; $server = $localhost unless $server; $requester = $localhost unless $requester; # debug¸¦ À§ÇÑ Ãâ·ÂÄÚµå #print "Debug code - \n"; #print "Server:$server\n"; #print "Port:$port\n"; #print "Parent nodes:$parents\n"; #print "Child nodes:$children\n"; #print "Keyword:$keyword\n"; #print "Requester:$requester\n"; #print "Maxhop:$maxhop\n"; # CGI FORMÀÇ Ãâ·Â(¼­ºê·çƾ È£Ãâ) &print_form($keyword); # ÀÌ¿ôÇÏ´Â »óÀ§ ¼­¹ö¿Í ÇÏÀ§ ¼­¹öÀÇ À̸§À» Ãâ·ÂÇÏ°í ¸µÅ©¸¦ ºÙÀδÙ. print "»óÀ§ ¼­¹ö :
    \n"; foreach $node (split /\s+/, $parents) { print "
  1. ¼­¹ö $node\n"; } print "
\n

\n"; print "ÇÏÀ§ ¼­¹ö :

    \n"; foreach $node (split /\s+/, $children) { print "
  1. ¼­¹ö $node\n"; } print "
\n

\n"; # °Ë»ö °á°ú°¡ ¸¹À» °æ¿ì °¢°¢ÀÇ ¼­¹ö¿¡¼­ ³ª¿Â °á°ú¿¡ ºÙ¿© ³õÀº ű׸¦ # »ç¿ëÇϱâ À§ÇÑ '¹Ù·Î °¡´Â ¸µÅ©'¸¦ ºÙÀδÙ. ±×·¯³ª ÀÌ ÇÁ·Î±×·¥¿¡¼­´Â # ±¸ÇöÀº µÇ¾úÀ¸³ª Á÷°üÀûÀÎ ÀÎÅÍÆäÀ̽º¸¦ À§ÇØ »ç¿ëÇÏÁö ¾Ê´Â´Ù. #print "°Ë»ö°á°ú·Î ¹Ù·Î °¡´Â ¸µÅ© :

    \n"; #print "
  1. $server·ÎºÎÅÍÀÇ °á°ú\n"; #foreach $node (split /\s+/, $parents) { # print "
  2. $node·ÎºÎÅÍÀÇ °á°ú\n "; #} #foreach $node (split /\s+/, $children) { # print "
  3. $node·ÎºÎÅÍÀÇ °á°ú\n "; #} #print "
\n

\n"; # ù¹ø°·Î ·ÎÄà ¼­¹ö¿¡¼­ °Ë»öÀ» ½ÃµµÇÑ´Ù. # ·ÎÄà ¼­¹ö¿¡ HTTP 80 Æ÷Æ®·Î ¿¬°áÀ» ¼³Á¤ÇÑ´Ù.(¼­ºê·çƾ È£Ãâ) &connection($server, $keyword, 'LOCAL', $server); # max hopÀ» °Ë»çÇÏ¿© ÀÏÁ¤ Ƚ¼ö ÀÌ»ó °Ë»ö ¿äûÀÌ ÁøÇàµÇ¸é ÁߴܽÃŲ´Ù. # ÀÌ ±â´ÉÀº µÎ °³ÀÇ ¼­¹ö°¡ À߸øµÈ ¼³Á¤À¸·Î ÀÎÇØ °Ë»ö ¿äûÀ» ¹«ÇÑÈ÷ # ¹Ýº¹ÇÏ´Â °ÍÀ» ¸·±â À§ÇÑ º¸Á¶ ÀåÄ¡ÀÌ´Ù. if ($maxhop > 4) { print "³Ê¹« ¸¹Àº ¼­¹ö¸¦ Ž»öÇÏ¿´À¸¹Ç·Î ¿©±â¼­ Áß´ÜÇÕ´Ï´Ù.
\n"; &print_tail; exit(0); } # µÎ¹ø°·Î´Â ÇÏÀ§ ¼­¹ö·ÎºÎÅÍ °Ë»öÀ» ½ÃµµÇÑ´Ù. ÇÏÀ§ ¼­¹ö´Â 2°³ ÀÌ»óÀÏ ¼ö ÀÖ´Ù. foreach $node (split /\s+/, $children) { if ($requester eq $node) { print "ÇÏÀ§ ¼­¹ö $node´Â °Ë»öÀ» ¿äûÇÑ ¼­¹öÀ̹ǷΠ¼­¹ö $server´Â ÇÏÀ§ ¼­¹öÀÎ $node¿¡°Ô °Ë»öÀ» ¿äûÇÏÁö ¾Ê½À´Ï´Ù.
\n"; } else { print "¼­¹ö $server°¡ ÇÏÀ§ ¼­¹ö $node¿¡ °Ë»öÀ» ¿äûÇÕ´Ï´Ù.
\n"; &connection($node, $keyword, 'REMOTE', $server); } } # ¼¼¹ø°·Î´Â »óÀ§ ¼­¹ö¿¡°Ô °Ë»öÀ» ¿äûÇÑ´Ù. »óÀ§ ¼­¹ö ¶ÇÇÑ 2°³ ÀÌ»ó ³ª¿­µÉ # ¼ö ÀÖ´Ù. foreach $node (split /\s+/, $parents) { if ($requester eq $node) { print "»óÀ§ ¼­¹ö $node´Â °Ë»öÀ» ¿äûÇÑ ¼­¹öÀ̹ǷΠ¼­¹ö $server´Â »óÀ§ ¼­¹öÀÎ $node¿¡°Ô °Ë»öÀ» ¿äûÇÏÁö ¾Ê½À´Ï´Ù.
\n"; } else { print "¼­¹ö $server°¡ »óÀ§ ¼­¹ö $node¿¡ °Ë»öÀ» ¿äûÇÕ´Ï´Ù.
\n"; &connection($node, $keyword, 'REMOTE', $server); } } # HTMLÀÇ ³ª¸ÓÁö tag¸¦ Ãâ·ÂÇÏ°í Á¾·áÇÑ´Ù. &print_tail; exit; # ¼­ºê·çƾ Á¤ÀÇ # Çì´õ Ãâ·Â sub print_head { print "Content-type: text/html\n\n"; print "\n"; print "\n"; } # FORM Ãâ·Â sub print_form { # keyword´Â FORMÀÇ °Ë»ö¾î ºÎºÐ¿¡ ¹Ì¸® ä¿ö ³ÖÀ» °Ë»ö¾î¸¦ ³Ñ°Ü ¹Þ´Â º¯¼ö my ($keyword) = @_; print "

Meta search

\n"; print "
\n"; print "°Ë»ö ´Ü¾î : \n"; print "\n"; print "
\n"; } # tail tag Ãâ·Â sub print_tail { print "\n"; print "\n"; } # SocketÀ» ÅëÇÑ connection sub connection { # °Ë»öÇÒ À¥¼­¹ö À̸§, °Ë»ö¾î, ·ÎÄÃÀΰ¡ ¸®¸ðÆ®Àΰ¡ÀÇ ¿©ºÎ, ¿äûÇÑ ¼­¹ö À̸§ my ($webserver, $word, $whereabout, $req) = @_; my ($iaddr, $paddr, $proto, $state, $line); # ¸®¸ðÆ®ÀÏ ¶§¿Í ·ÎÄÃÀÏ ¶§ °¢°¢ÀÇ °Ë»öÀ» ¹Þ¾ÆµéÀÏ CGIÇÁ·Î±×·¥ÀÇ À§Ä¡ ÁöÁ¤ if ($whereabout eq 'REMOTE') { # In remote hosts, 'm_search.cgi' call another 'm_search.cgi'. $maxhop++; $location = "/~terzeron/meta/m_search.cgi?keyword=$word&requester=$req&maxhop=$maxhop"; # remotehost file location } else { # In localhost, 'm_search.cgi' use 'htdig'. $location = "/cgi-bin/htsearch?config=htdig&restrict=&exclue=&method=and&format=builtin-long&words=$word"; # localhost file location } # '¹Ù·Î °¡´Â ¸µÅ©'¸¦ À§ÇÑ ÅÂ±× Ãâ·Â(»ç¿ëµÇÁö ¾Ê´Â´Ù) print "\n"; print "$webserver¿¡¼­ÀÇ $word¿¡ ´ëÇÑ °Ë»ö °á°ú
\n"; # socket ¼³Á¤ if ($port =~ /\D/) { $port = getservbyname($port, 'tcp'); } print "No port" unless $port; $iaddr = inet_aton($webserver) or print "no host: $webserver"; $paddr = sockaddr_in($port, $iaddr); print "No port address" unless $paddr; $proto = getprotobyname('tcp'); print "No protocol" unless $proto; socket(SOCK, PF_INET, SOCK_STREAM, $proto) or print "socket: $!"; connect(SOCK, $paddr) or print "connect: $!"; send SOCK, "GET $location\n", MSG_DONTROUTE or print "can't send message to web server\n"; # htdigÀÇ htsearch¿¡¼­ ³ª¿À´Â HTML ű׸¦ °¡Áö°í °Ë»ö °á°ú¸¦ Ãß·Á³½´Ù. $state = 0; while () { if ($whereabout eq 'LOCAL') { $state = 2 if /\
°æ°í! "; print "$webserverÀÇ Meta search engineÀÌ "; print "$req·ÎºÎÅÍÀÇ " if $whereabout eq 'REMOTE'; print "°Ë»ö¾î \"$word\"¿¡ °ü·ÃµÈ "; print "¾î¶² ¹®¼­µµ ãÁö ¸øÇß½À´Ï´Ù.\n"; } print "
\n"; }