summaryrefslogtreecommitdiffstats
path: root/src/tor_ioslave/torioslave.cpp
blob: c3fc8b058540c65e77327557ef19c1f85fc12e9c (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
/***************************************************************************
** $Id: torioslave.cpp,v 1.7 2008/07/31 19:56:29 hoganrobert Exp $
 *   Copyright (C) 2006 - 2008 Robert Hogan                                *
 *   [email protected]                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 ***************************************************************************/

#include <tqcstring.h>
#include <tqsocket.h>
#include <tqdatetime.h>
#include <tqbitarray.h>

#include <tdeapplication.h>
#include <kdebug.h>
#include <tdemessagebox.h>
#include <kinstance.h>
#include <tdeglobal.h>
#include <kstandarddirs.h>
#include <tdelocale.h>
#include <kurl.h>
#include <ksock.h>
#include <dcopref.h>
#include <dcopclient.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <krun.h>
#include <kprocio.h>

#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>

#include "torioslave.h"

using namespace TDEIO;


tdeio_torProtocol::tdeio_torProtocol(const TQCString &pool_socket, const TQCString &app_socket)
    : SlaveBase("tdeio_tor", pool_socket, app_socket)
{
}


tdeio_torProtocol::~tdeio_torProtocol()
{
}


void tdeio_torProtocol::get(const KURL& url )
{

    TQString wait;
    TQByteArray output;

    TQTextStream os( output, IO_WriteOnly );
    os.setEncoding( TQTextStream::Latin1 ); // In fast ASCII

    TQString cleanedurl = url.prettyURL().replace("tor://","");
    cleanedurl = cleanedurl.replace("tor:/","");
    cleanedurl = cleanedurl.replace("tor:","");
    cleanedurl = cleanedurl.replace(" ","");
    bool cantdo = false;

    if (cleanedurl.contains(".txt"))
        cantdo = true;

    if (cleanedurl.contains("://") && (cleanedurl.left(4) != "http"))
        cantdo = true;

    if (cantdo){

        os << TQString("<html><head><title>Tor</title></head><body>%1 is not a valid URL for this feature. Websites only I'm afraid. And no file extensions either!</body></html>").arg(cleanedurl);
    
        data( output );
        finished();
        return;
    }

    if ((cleanedurl.left(7) != "http://") && (cleanedurl.left(8) != "https://"))
        cleanedurl.prepend("http://");

    bool m_torkrunning = false;
    bool anonymized = false;

    DCOPClient* p_dcopServer= new DCOPClient();
    p_dcopServer->attach ();

    if (p_dcopServer->isApplicationRegistered ("tork")){
            m_torkrunning = true;
            DCOPRef tork( "tork", "DCOPTork" );
            anonymized = tork.call( "getTDESetting" );
    }

    

    if (m_torkrunning){
        DCOPRef("tork*", "DCOPTork").send("startEverything");
        if (!anonymized)
            DCOPRef("tork*", "DCOPTork").send("toggleTDESetting");
        wait = "3";
    }else{
        KProcIO *whichproc = new KProcIO();
        whichproc->setUseShell(TRUE);
        TQString whichCommand="tork --toggleTDE";
    
        *whichproc<<whichCommand;
    
        whichproc->start(KProcIO::NotifyOnExit,TRUE);

        //KRun::runCommand( "tork --toggleTDE");
        wait = "10";
    }


    os << TQString("<html><head><title>Tor</title><META HTTP-EQUIV='Refresh' CONTENT='%1; URL=%2'></head><body>Will load %3 anonymously in a moment.</body></html>").arg(wait).arg(cleanedurl).arg(cleanedurl);

    data( output );
    finished();

}


void tdeio_torProtocol::mimetype(const KURL & /*url*/)
{
    mimeType("text/html");
    finished();
}


extern "C"
{
    int kdemain(int argc, char **argv)
    {
        TDEInstance instance( "tdeio_tor" );
        
        kdDebug(7101) << "*** Starting tdeio_tor " << endl;
        
        if (argc != 4) {
            kdDebug(7101) << "Usage: tdeio_tor  protocol domain-socket1 domain-socket2" << endl;
            exit(-1);
        }
        
        tdeio_torProtocol slave(argv[2], argv[3]);
        slave.dispatchLoop();
        
        kdDebug(7101) << "*** tdeio_tor Done" << endl;
        return 0;
    }
}