Total Pageviews

Sunday 10 April 2016

“简单的翻墙术-一个基于perl的脚本”的升级版

这几天做了个可以偶尔用用的版本——说偶尔用用是因为bug还不少。而且我没有在代码中增加SSL支持,因此经常会被GFW切断,不过反正说了是偶尔用用就无所谓了。最主要的是因为自己没有需求,做这个东西只是练手,试用POE,虽然到最后麻烦点落到了操作DOM,HTML里面能写链接的地方还真不少啊,得一个一个改。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use POE;
use POE::Component::Server::TCP;
use POE::Filter::HTTPD;
use POE::Filter::Stream;
use HTTP::Response;
use HTML::TreeBuilder;
use URI;
use LWP::UserAgent;
 
my $base_domain = '.icylife.net:8443';
 
POE::Component::Server::TCP->new( Port => 8443,
                                ClientFilter => 'POE::Filter::HTTPD',
                                ClientInput => \&handle_http_request,
                        );
 
POE::Kernel->run();
exit( );
 
sub replace_link
{
        my $dom_item = shift;
        my $attr_name = shift;
        my $base_url = shift;
 
        if( !defined($dom_item) || !defined($attr_name) || !defined($base_url) || !defined($dom_item->attr($attr_name)) )
        {
                return;
        }
 
        my $uri = URI->new( $dom_item->attr($attr_name) );
 
        $uri = $uri->abs($base_url);
        $uri->host($uri->host.$base_domain);
        $dom_item->attr($attr_name, $uri->as_string);
}
 
sub process_html
{
        my $html = shift;
        my $base_url = shift;
 
        my $root = HTML::TreeBuilder->new;
        $root->no_space_compacting( 1 );
        $root->ignore_ignorable_whitespace( 0 );
 
        $html =~ s/ / /g;
        $root->parse_content( $html );
        #$root->dump;
 
        # handle links
        my @links = $root->look_down( _tag=>'a', sub{ defined($_[0]->attr('href')) } );
        foreach my $link( @links )
        {
                my $old_link = $link->attr('href');
                if( $old_link =~ /^http:\/\// && defined($old_link) && $old_link !~ /^javascript:/ && $old_link !~ /^mailto:/ && $old_link !~ /^#/ )
                {
                        replace_link( $link, 'href', $base_url );
                }
        }
 
        my @csss = $root->look_down( _tag=>'link', sub{ defined($_[0]->attr('href')) } );
        foreach my $css( @csss )
        {
                replace_link( $css, 'href', $base_url );
        }
 
        my @jss = $root->look_down( _tag=>'script', sub{ defined($_[0]->attr('src')) } );
        foreach my $js( @jss )
        {
                replace_link( $js, 'src', $base_url );
        }
 
        my @pics = $root->look_down( _tag=>'img', sub{ defined($_[0]->attr('src')) } );
        foreach my $pic( @pics )
        {
                replace_link( $pic, 'src', $base_url );
        }
 
        my @forms = $root->look_down( _tag=>'form', sub{ defined($_[0]->attr('action')) } );
        foreach my $form( @forms )
        {
                replace_link( $form, 'action', $base_url );
        }
 
        my $new_html = $root->as_HTML( '' );
        $root->delete;
 
        #$new_html =~ s!^<body>\n?!!;
        #$new_html =~ s!</body>\s*\z!!;
 
        return $new_html;
}
 
sub handle_http_response
{
        my $kernel = shift;
        my $heap = shift;
        my $response = shift;
 
        my $response_header = $response->{_headers};
        my $response_data = $response->content;
 
        $heap->{client}->set_output_filter(POE::Filter::Stream->new());
 
        if( lc($response_header->header('Content-Type')) =~ /text\/html/ )
        {
                $response_data = process_html( $response_data, $response->request->uri );
        }
        $response->{_content} = $response_data;
        delete $response->{_request};
 
        $heap->{client}->put($response->as_string);
 
        print "sent ".length($response_data)." bytes\n";
 
        $kernel->yield("shutdown");
}
 
sub handle_http_request
{
        my ( $kernel, $heap, $request ) = @_[ KERNEL, HEAP, ARG0 ];
 
        if ( $request->isa("HTTP::Response") )
        {
                $heap->{client}->put($request);
                $kernel->yield("shutdown");
 
                return;
        }
 
        my $new_host = $request->header( "host" );
        $new_host =~ s/\.icylife\.net:8443//g;
        $request->header( "host", $new_host );
 
        $request->uri( "http://$new_host".$request->uri );
        $request->header("Connection", "close");
        $request->header("Proxy-Connection", "close");
        $request->remove_header("Keep-Alive");
        $request->remove_header("Accept-Encoding");
 
        print "access ".$request->uri->as_string."\n";
 
        my $ua = LWP::UserAgent->new;
        $ua->timeout(10);
 
        my $response = $ua->request( $request );
 
        handle_http_response( $kernel, $heap, $response );
}

from http://web.archive.org/web/20131028230847/http://www.icylife.net/blog/?p=865

相关帖子:

简单的翻墙术-一个基于perl的脚本