Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
bytevia
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Michael Pastushkov
bytevia
Commits
0e0ac9ea
Commit
0e0ac9ea
authored
Sep 20, 2024
by
Michael Pastushkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
java work in progress
parent
6337ac45
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
416 additions
and
78 deletions
+416
-78
java/ByteVia copy.java
+225
-0
java/ByteVia$Options.class
+0
-0
java/ByteVia.class
+0
-0
java/ByteVia.java
+189
-76
run_tcp_client
+1
-1
src/bytevia.c
+1
-1
No files found.
java/ByteVia copy.java
0 → 100644
View file @
0e0ac9ea
/*
* Copyright (C) 2024 Michael Pastushkov <michael@pastushkov.com>
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*/
import
java.net.*
;
import
java.io.*
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
public
class
ByteVia
{
private
static
final
int
MAX_UDP_CLIENTS
=
10
;
private
byte
[]
cipher
=
new
byte
[
256
];
private
int
cipherTime
;
private
Options
options
=
new
Options
();
private
DatagramSocket
udpSocket
;
private
ServerSocket
tcpSocket
;
private
Map
<
InetAddress
,
Integer
>
clients
=
new
HashMap
<>();
public
static
void
main
(
String
[]
args
)
{
ByteVia
server
=
new
ByteVia
();
server
.
initOptions
();
server
.
run
();
}
public
void
initOptions
()
{
// Initialize default options
options
.
localPort
=
1948
;
options
.
bindAddress
=
"0.0.0.0"
;
options
.
remoteHost
=
"p4pn.net"
;
options
.
remotePort
=
1984
;
options
.
bufferSize
=
4096
;
options
.
encrypt
=
false
;
options
.
log
=
true
;
options
.
secret
=
52341
;
options
.
proto
=
"tcp"
;
options
.
mode
=
"client"
;
}
public
void
run
()
{
try
{
if
(
options
.
log
)
{
System
.
out
.
println
(
"NetVia Java"
);
System
.
out
.
println
(
" proto: "
+
options
.
proto
);
System
.
out
.
println
(
" local: "
+
options
.
bindAddress
+
":"
+
options
.
localPort
);
System
.
out
.
println
(
" remote: "
+
options
.
remoteHost
+
":"
+
options
.
remotePort
);
System
.
out
.
println
(
" mode: "
+
options
.
mode
);
System
.
out
.
println
(
" encrypt: "
+
options
.
encrypt
);
}
if
(
options
.
proto
==
"udp"
)
{
udpSocket
=
new
DatagramSocket
(
options
.
localPort
);
System
.
out
.
println
(
"UDP server started on port "
+
options
.
localPort
);
udpServerLoop
();
}
else
{
tcpSocket
=
new
ServerSocket
(
options
.
localPort
);
System
.
out
.
println
(
"TCP server started on port "
+
options
.
localPort
);
tcpServerLoop
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
private
void
udpServerLoop
()
{
byte
[]
buffer
=
new
byte
[
options
.
bufferSize
];
DatagramPacket
packet
=
new
DatagramPacket
(
buffer
,
buffer
.
length
);
while
(
true
)
{
try
{
udpSocket
.
receive
(
packet
);
InetAddress
clientAddress
=
packet
.
getAddress
();
int
clientPort
=
packet
.
getPort
();
System
.
out
.
println
(
getCurrentTimestamp
()
+
" Received packet from "
+
clientAddress
);
// Add client to list if not already
if
(!
clients
.
containsKey
(
clientAddress
))
{
clients
.
put
(
clientAddress
,
clientPort
);
}
// Process data (encryption simulation)
if
(
options
.
encrypt
)
{
updateCipher
();
for
(
int
i
=
0
;
i
<
packet
.
getLength
();
i
++)
{
buffer
[
i
]
=
cipher
[
buffer
[
i
]
&
0xFF
];
}
}
// Echo back the processed data
DatagramPacket
response
=
new
DatagramPacket
(
buffer
,
packet
.
getLength
(),
clientAddress
,
clientPort
);
udpSocket
.
send
(
response
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
private
void
tcpServerLoop
()
{
while
(
true
)
{
try
{
Socket
clientSocket
=
tcpSocket
.
accept
();
System
.
out
.
println
(
getCurrentTimestamp
()
+
" Accepted connection from "
+
clientSocket
.
getInetAddress
());
new
Thread
(()
->
handleTcpClient
(
clientSocket
)).
start
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
private
void
handleTcpClient
(
Socket
clientSocket
)
{
try
{
Socket
remoteSocket
=
new
Socket
();
remoteSocket
.
connect
(
new
InetSocketAddress
(
options
.
remoteHost
,
options
.
remotePort
));
System
.
out
.
println
(
"Connected to remote server at "
+
options
.
remoteHost
+
":"
+
options
.
remotePort
);
InputStream
in
=
clientSocket
.
getInputStream
();
OutputStream
out
=
remoteSocket
.
getOutputStream
();
InputStream
in2
=
remoteSocket
.
getInputStream
();
OutputStream
out2
=
clientSocket
.
getOutputStream
();
byte
[]
buffer
=
new
byte
[
options
.
bufferSize
];
int
bytesRead
;
while
(
true
)
{
if
(
in
.
available
()
>
0
)
{
bytesRead
=
in
.
read
(
buffer
);
if
(
bytesRead
==
-
1
)
break
;
System
.
out
.
println
(
" From client "
+
bytesRead
+
" bytes"
);
// Process data (encryption simulation)
if
(
options
.
encrypt
)
{
updateCipher
();
for
(
int
i
=
0
;
i
<
bytesRead
;
i
++)
{
buffer
[
i
]
=
cipher
[
buffer
[
i
]
&
0xFF
];
}
}
out
.
write
(
buffer
,
0
,
bytesRead
);
out
.
flush
();
System
.
out
.
println
(
" Sent"
);
}
/*******/
if
(
in2
.
available
()
>
0
)
{
bytesRead
=
in2
.
read
(
buffer
);
if
(
bytesRead
==
-
1
)
break
;
System
.
out
.
println
(
" From remote "
+
bytesRead
+
" bytes"
);
out2
.
write
(
buffer
,
0
,
bytesRead
);
out2
.
flush
();
System
.
out
.
println
(
" Sent"
);
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
private
void
updateCipher
()
{
int
currentTime
=
getTime
();
if
(
currentTime
==
cipherTime
)
{
return
;
}
Random
random
=
new
Random
(
currentTime
);
for
(
int
i
=
0
;
i
<
256
;
i
++)
{
cipher
[
i
]
=
(
byte
)
i
;
}
for
(
int
i
=
255
;
i
>
0
;
i
--)
{
int
j
=
random
.
nextInt
(
i
+
1
);
byte
temp
=
cipher
[
i
];
cipher
[
i
]
=
cipher
[
j
];
cipher
[
j
]
=
temp
;
}
cipherTime
=
currentTime
;
System
.
out
.
println
(
getCurrentTimestamp
()
+
" Updated cipher."
);
}
private
int
getTime
()
{
return
(
int
)
(
System
.
currentTimeMillis
()
/
(
60
*
10
*
1000
));
}
private
String
getCurrentTimestamp
()
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
return
sdf
.
format
(
new
Date
());
}
static
class
Options
{
int
localPort
;
String
bindAddress
;
String
remoteHost
;
int
remotePort
;
int
bufferSize
;
boolean
encrypt
;
boolean
log
;
int
secret
;
String
proto
;
String
mode
;
}
}
java/ByteVia$Options.class
View file @
0e0ac9ea
No preview for this file type
java/ByteVia.class
View file @
0e0ac9ea
No preview for this file type
java/ByteVia.java
View file @
0e0ac9ea
...
...
@@ -22,145 +22,256 @@ import java.net.*;
import
java.io.*
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
import
java.time.Instant
;
import
java.nio.ByteBuffer
;
import
java.util.Iterator
;
import
java.nio.channels.*
;
import
java.util.Set
;
import
java.nio.channels.spi.*
;
public
class
ByteVia
{
private
static
final
int
MAX_UDP_CLIENTS
=
10
;
private
byte
[]
cipher
=
new
byte
[
256
];
private
int
cipherTime
;
private
Options
options
=
new
Options
();
private
DatagramSocket
udpSocket
;
private
ServerSocket
tcpSocket
;
private
Map
<
InetAddress
,
Integer
>
clients
=
new
HashMap
<>();
private
int
cipherTime
=
-
1
;
public
static
void
main
(
String
[]
args
)
{
ByteVia
server
=
new
ByteVia
();
server
.
initOptions
();
server
.
run
();
ByteVia
bytevia
=
new
ByteVia
();
bytevia
.
initOptions
();
bytevia
.
run
();
}
public
void
initOptions
()
{
// Initialize default options
options
.
localPort
=
1948
;
options
.
remoteHost
=
"127.0.0.1"
;
options
.
bindAddress
=
"0.0.0.0"
;
options
.
remoteHost
=
"p4pn.net"
;
options
.
remotePort
=
1984
;
options
.
bufferSize
=
4096
;
options
.
encrypt
=
fals
e
;
options
.
log
=
true
;
options
.
encrypt
=
tru
e
;
options
.
log
=
1
;
options
.
secret
=
52341
;
options
.
proto
=
"tcp"
;
options
.
mode
=
"client"
;
}
public
void
run
()
{
if
(
options
.
log
>
0
)
{
System
.
out
.
println
(
"NetVia Java"
);
System
.
out
.
println
(
" proto: "
+
options
.
proto
);
System
.
out
.
println
(
" local: "
+
options
.
bindAddress
+
":"
+
options
.
localPort
);
System
.
out
.
println
(
" remote: "
+
options
.
remoteHost
+
":"
+
options
.
remotePort
);
System
.
out
.
println
(
" mode: "
+
options
.
mode
);
System
.
out
.
println
(
" encrypt: "
+
options
.
encrypt
);
}
try
{
if
(
"udp"
.
equalsIgnoreCase
(
options
.
proto
))
{
udpSocket
=
new
DatagramSocket
(
options
.
localPort
);
System
.
out
.
println
(
"UDP server started on port "
+
options
.
localPort
);
udpServerLoop
();
}
else
if
(
"tcp"
.
equalsIgnoreCase
(
options
.
proto
))
{
tcpSocket
=
new
ServerSocket
(
options
.
localPort
);
System
.
out
.
println
(
"TCP server started on port "
+
options
.
localPort
);
tcpServerLoop
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
private
void
udpServerLoop
()
{
byte
[]
buffer
=
new
byte
[
options
.
bufferSize
];
DatagramPacket
packet
=
new
DatagramPacket
(
buffer
,
buffer
.
length
);
while
(
true
)
{
private
void
tcpServerLoop
()
{
try
{
udpSocket
.
receive
(
packet
);
InetAddress
clientAddress
=
packet
.
getAddress
();
int
clientPort
=
packet
.
getPort
();
System
.
out
.
println
(
getCurrentTimestamp
()
+
" Received packet from "
+
clientAddress
);
// Add client to list if not already
if
(!
clients
.
containsKey
(
clientAddress
))
{
clients
.
put
(
clientAddress
,
clientPort
);
ServerSocketChannel
serverSocketChannel
=
ServerSocketChannel
.
open
();
serverSocketChannel
.
socket
().
bind
(
new
InetSocketAddress
(
1948
));
while
(
true
)
{
SocketChannel
clientChannel
=
serverSocketChannel
.
accept
();
System
.
out
.
println
(
"Accepted connection"
);
handleTcpClient
(
clientChannel
);
}
// Process data (encryption simulation)
if
(
options
.
encrypt
)
{
updateCipher
();
for
(
int
i
=
0
;
i
<
packet
.
getLength
();
i
++)
{
buffer
[
i
]
=
cipher
[
buffer
[
i
]
&
0xFF
];
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
// Echo back the processed data
DatagramPacket
response
=
new
DatagramPacket
(
buffer
,
packet
.
getLength
(),
clientAddress
,
clientPort
);
udpSocket
.
send
(
response
);
private
void
handleTcpClient
(
SocketChannel
clientChannel
)
{
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
try
{
SocketChannel
remoteChannel
=
SocketChannel
.
open
();
remoteChannel
.
connect
(
new
InetSocketAddress
(
options
.
remoteHost
,
options
.
remotePort
));
System
.
out
.
println
(
"Connected to remote server at "
+
options
.
remoteHost
+
":"
+
options
.
remotePort
);
ByteBuffer
buffer
=
ByteBuffer
.
allocate
(
options
.
bufferSize
);
Selector
selector
=
SelectorProvider
.
provider
().
openSelector
();
clientChannel
.
configureBlocking
(
false
);
remoteChannel
.
configureBlocking
(
false
);
clientChannel
.
register
(
selector
,
SelectionKey
.
OP_READ
,
"client"
);
remoteChannel
.
register
(
selector
,
SelectionKey
.
OP_READ
,
"remote"
);
while
(
true
)
{
selector
.
select
();
Set
<
SelectionKey
>
selectedKeys
=
selector
.
selectedKeys
();
Iterator
<
SelectionKey
>
iterator
=
selectedKeys
.
iterator
();
while
(
iterator
.
hasNext
())
{
SelectionKey
key
=
iterator
.
next
();
iterator
.
remove
();
if
(
key
.
isReadable
())
{
// Check which channel is ready and read data
SocketChannel
channel
=
(
SocketChannel
)
key
.
channel
();
String
streamId
=
(
String
)
key
.
attachment
();
buffer
.
clear
();
int
bytesRead
=
channel
.
read
(
buffer
);
if
(
bytesRead
>
0
)
{
System
.
out
.
println
(
"Data read from "
+
streamId
+
", bytes: "
+
bytesRead
);
buffer
.
flip
();
byte
[]
data
=
new
byte
[
buffer
.
position
()];
buffer
.
flip
();
// Switch buffer to reading mode
buffer
.
get
(
data
);
// Transfer buffer content to byteArray
if
(
streamId
==
"client"
)
{
encode
(
data
);
buffer
=
ByteBuffer
.
wrap
(
data
);
buffer
.
flip
();
while
(
buffer
.
hasRemaining
())
{
remoteChannel
.
write
(
buffer
);
}
}
else
{
decode
(
data
);
buffer
=
ByteBuffer
.
wrap
(
data
);
buffer
.
flip
();
while
(
buffer
.
hasRemaining
())
{
clientChannel
.
write
(
buffer
);
}
}
private
void
tcpServerLoop
()
{
while
(
true
)
{
try
{
Socket
clientSocket
=
tcpSocket
.
accept
();
System
.
out
.
println
(
getCurrentTimestamp
()
+
" Accepted connection from "
+
clientSocket
.
getInetAddress
());
new
Thread
(()
->
handleTcpClient
(
clientSocket
)).
start
();
}
else
if
(
bytesRead
==
-
1
)
{
// End of stream, close the channel
key
.
cancel
();
channel
.
close
();
}
}
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
private
int
xorshift32
(
int
[]
state
)
{
int
x
=
state
[
0
];
x
^=
(
x
<<
13
);
x
^=
(
x
>>>
17
);
// Unsigned right shift
x
^=
(
x
<<
5
);
state
[
0
]
=
x
;
//System.out.printf("xor: %d \n", x);
return
x
;
}
private
void
handleTcpClient
(
Socket
clientSocket
)
{
try
(
InputStream
in
=
clientSocket
.
getInputStream
();
OutputStream
out
=
clientSocket
.
getOutputStream
())
{
byte
[]
buffer
=
new
byte
[
options
.
bufferSize
];
private
void
shuffle
(
byte
[]
array
,
int
seed
)
{
int
bytesRead
;
while
((
bytesRead
=
in
.
read
(
buffer
))
!=
-
1
)
{
System
.
out
.
println
(
getCurrentTimestamp
()
+
" Received "
+
bytesRead
+
" bytes"
)
;
int
n
=
array
.
length
;
int
[]
state
=
new
int
[
1
];
state
[
0
]
=
seed
;
// Process data (encryption simulation)
if
(
options
.
encrypt
)
{
updateCipher
();
for
(
int
i
=
0
;
i
<
bytesRead
;
i
++)
{
buffer
[
i
]
=
cipher
[
buffer
[
i
]
&
0xFF
];
for
(
int
i
=
n
-
1
;
i
>
0
;
i
--)
{
int
x
=
xorshift32
(
state
);
int
j
=
(
x
&
0xFFFFFFFF
)
%
(
i
+
1
);
System
.
out
.
printf
(
"x: %s, j: %s, i: %s \n"
,
Integer
.
toBinaryString
(
x
),
Integer
.
toBinaryString
(
j
),
Integer
.
toBinaryString
(
i
));
byte
temp
=
array
[
i
];
array
[
i
]
=
array
[
j
];
array
[
j
]
=
temp
;
}
}
out
.
write
(
buffer
,
0
,
bytesRead
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
private
int
getHash
(
int
source
)
{
int
hash
=
source
;
hash
=
(
hash
^
61
)
^
(
hash
>>>
16
);
// Unsigned right shift
hash
=
hash
+
(
hash
<<
3
);
hash
=
hash
^
(
hash
>>>
4
);
// Unsigned right shift
hash
=
hash
*
0x27d4eb2d
;
hash
=
hash
^
(
hash
>>>
15
);
// Unsigned right shift
return
hash
;
}
private
int
getTime
()
{
long
now
=
Instant
.
now
().
getEpochSecond
();
// Get current time in seconds since UNIX epoch
return
(
int
)
(
now
/
(
60
*
10
));
// Change cipher every 10 minutes
}
private
void
updateCipher
()
{
int
currentT
ime
=
getTime
();
if
(
currentT
ime
==
cipherTime
)
{
int
t
ime
=
getTime
();
if
(
t
ime
==
cipherTime
)
{
return
;
}
Random
random
=
new
Random
(
currentTime
);
for
(
int
i
=
0
;
i
<
256
;
i
++)
{
cipher
[
i
]
=
(
byte
)
i
;
}
for
(
int
i
=
255
;
i
>
0
;
i
--)
{
int
j
=
random
.
nextInt
(
i
+
1
);
byte
temp
=
cipher
[
i
];
cipher
[
i
]
=
cipher
[
j
];
cipher
[
j
]
=
temp
;
int
seed
=
getHash
(
options
.
secret
*
time
);
shuffle
(
cipher
,
seed
);
if
(
options
.
log
>
1
)
{
System
.
out
.
println
(
" new cipher "
+
time
);
if
(
options
.
log
>
2
)
{
for
(
int
i
=
0
;
i
<
256
;
i
++)
{
System
.
out
.
print
(
cipher
[
i
]
+
" "
);
if
((
i
+
1
)
%
16
==
0
)
{
System
.
out
.
println
();
}
}
}
}
cipherTime
=
time
;
}
private
int
encode
(
byte
[]
buf
)
{
if
(!
options
.
encrypt
)
return
0
;
updateCipher
();
int
len
=
buf
.
length
;
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
buf
[
i
]
=
cipher
[
buf
[
i
]
&
0xFF
];
// Ensure byte is treated as unsigned
}
if
(
options
.
log
>
0
)
{
System
.
out
.
printf
(
"\r%-50s"
,
" "
);
System
.
out
.
printf
(
"\r%s encode %d bytes "
,
getCurrentTimestamp
(),
len
);
System
.
out
.
flush
();
}
cipherTime
=
currentTime
;
System
.
out
.
println
(
getCurrentTimestamp
()
+
" Updated cipher."
);
return
0
;
}
private
int
getTime
()
{
return
(
int
)
(
System
.
currentTimeMillis
()
/
(
60
*
10
*
1000
));
private
int
decode
(
byte
[]
buf
)
{
if
(!
options
.
encrypt
)
{
return
0
;
}
updateCipher
();
int
len
=
buf
.
length
;
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
for
(
int
j
=
0
;
j
<
256
;
j
++)
{
if
(
cipher
[
j
]
==
buf
[
i
])
{
buf
[
i
]
=
(
byte
)
j
;
break
;
}
}
}
if
(
options
.
log
>
0
)
{
System
.
out
.
printf
(
"\r%-50s"
,
" "
);
System
.
out
.
printf
(
"\r%s decode %d bytes "
,
getCurrentTimestamp
(),
len
);
System
.
out
.
flush
();
}
return
0
;
}
private
String
getCurrentTimestamp
()
{
...
...
@@ -169,13 +280,15 @@ public class ByteVia {
}
static
class
Options
{
String
proto
;
int
localPort
;
String
bindAddress
;
String
remoteHost
;
int
remotePort
;
int
bufferSize
;
boolean
encrypt
;
boolean
log
;
int
log
;
int
secret
;
String
proto
;
String
mode
;
}
}
run_tcp_client
View file @
0e0ac9ea
#!/bin/sh
make clean
&&
make
&&
./bytevia
--local-port
=
1948
--remote-host
=
p4pn.net
--remote-port
=
1984
--proto
=
tcp
--encrypt
=
0
make clean
&&
make
&&
./bytevia
--local-port
=
1948
--remote-host
=
p4pn.net
--remote-port
=
1984
--proto
=
tcp
--encrypt
=
1
src/bytevia.c
View file @
0e0ac9ea
...
...
@@ -90,7 +90,7 @@ unsigned int xorshift32(unsigned int *state)
void
shuffle
(
unsigned
char
*
array
,
int
n
,
unsigned
int
seed
)
{
unsigned
int
state
=
seed
;
srand
(
seed
);
//
srand(seed);
for
(
int
i
=
n
-
1
;
i
>
0
;
i
--
)
{
int
j
=
xorshift32
(
&
state
)
%
(
i
+
1
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment