Skip to content

Commit

Permalink
refactor: Refactoring LipiLee#1
Browse files Browse the repository at this point in the history
Refactoring LipiLee#1
  • Loading branch information
Jungwoon committed Jan 25, 2019
1 parent c120064 commit 09707d3
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 32 deletions.
4 changes: 0 additions & 4 deletions app/src/main/java/com/lipisoft/toyshark/PacketManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ enum class PacketManager {
return list.add(packet)
}

fun getList(): List<Packet> {
return list
}

fun setAdapter(adapter: PacketListAdapter) {
this.adapter = adapter
}
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/lipisoft/toyshark/ToySharkVPNService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ class ToySharkVPNService : VpnService(), Handler.Callback, Runnable, IProtectSoc
}

Log.i(TAG, "startVpnService => create builder")

// Configure a builder while parsing the parameters.
val builder = Builder()
.addAddress("10.120.0.1", 32)
Expand All @@ -350,12 +351,12 @@ class ToySharkVPNService : VpnService(), Handler.Callback, Runnable, IProtectSoc

fileDescriptor = builder.establish()

if (fileDescriptor != null) {
return if (fileDescriptor != null) {
Log.i(TAG, "VPN Established:interface = " + fileDescriptor!!.fileDescriptor.toString())
return true
true
} else {
Log.d(TAG, "fileDescriptor is null")
return false
false
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object IPPacketFactory {
iPv4Header.totalLength,
iPv4Header.identification,
iPv4Header.isMayFragment,
iPv4Header.isLastFragment,
iPv4Header.lastFragment,
iPv4Header.fragmentOffset,
iPv4Header.timeToLive,
iPv4Header.protocol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class IPv4Header(
var totalLength: Int,
var identification: Int,
private var mayFragment: Boolean,
val isLastFragment: Boolean,
val lastFragment: Boolean,
val fragmentOffset: Short,
val timeToLive: Byte,
val protocol: Byte,
Expand All @@ -68,7 +68,7 @@ class IPv4Header(
if (mayFragment)
this.flag = this.flag or 0x40

if (isLastFragment)
if (lastFragment)
this.flag = this.flag or 0x20
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import java.util.Date
internal class SocketDataReaderWorker(private val writer: ClientPacketWriter, private val sessionKey: String) : Runnable {
private val socketData: SocketData = SocketData.instance

companion object {
private const val TAG = "SocketDataReaderWorker"
}

override fun run() {
val session = SessionManager.INSTANCE.getSessionByKey(sessionKey)
if (session == null) {
Expand Down Expand Up @@ -127,6 +131,7 @@ internal class SocketDataReaderWorker(private val writer: ClientPacketWriter, pr
val data = ByteArray(dataSize)
System.arraycopy(buffer.array(), 0, data, 0, dataSize)
session.addReceivedData(data)

// pushing all data to vpn client
while (session.hasReceivedData()) {
pushDataToClient(session)
Expand All @@ -140,7 +145,7 @@ internal class SocketDataReaderWorker(private val writer: ClientPacketWriter, pr
*/
private fun pushDataToClient(session: Session) {
if (!session.hasReceivedData()) {
//no data to send
// no data to send
Log.d(TAG, "no data for vpn client")
}

Expand Down Expand Up @@ -243,8 +248,4 @@ internal class SocketDataReaderWorker(private val writer: ClientPacketWriter, pr
}

}

companion object {
private val TAG = "SocketDataReaderWorker"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.util.Date
class SocketDataWriterWorker(writer: ClientPacketWriter, private val sessionKey: String) : Runnable {

companion object {
private val TAG = "SocketDataWriterWorker"
private const val TAG = "SocketDataWriterWorker"
private var writer: ClientPacketWriter? = null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class SocketNIODataService(private val clientPacketWriter: ClientPacketWriter) :
if (shutdown) {
break
}

synchronized(syncSelector2) {
val iterator = selector!!.selectedKeys().iterator()
while (iterator.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ class SocketProtector {
protector!!.protectSocket(socket)
}

fun protect(socket: Int) {
protector!!.protectSocket(socket)
}

fun protect(socket: DatagramSocket) {
protector!!.protectSocket(socket)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ object UDPPacketFactory {
return UDPHeader(srcPort, destPort, length, checksum)
}

fun copyHeader(header: UDPHeader): UDPHeader {
return UDPHeader(header.sourcePort,
header.destinationPort,
header.length,
header.checksum)
}

/**
* create packet data for responding to vpn client
*
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/lipisoft/toyshark/util/ByteUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ class ByteUtils {
* @param num target number will be changed
* @return flipped representation.
*/
fun flip16(num: Int): Int {
private fun flip16(num: Int): Int {
return (num and 0x00FF shl 8) + (num and 0xFF00 shr 8)
}

Expand Down
6 changes: 2 additions & 4 deletions app/src/main/java/com/lipisoft/toyshark/util/PacketUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
package com.lipisoft.toyshark.util

import java.net.Inet4Address
import java.net.InetAddress
import java.net.NetworkInterface
import java.net.SocketException
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.util.Enumeration
import android.util.Log

import com.lipisoft.toyshark.network.ip.IPv4Header
Expand Down Expand Up @@ -296,7 +294,7 @@ object PacketUtil {
"\r\nIP Header length: " + ipheader.ipHeaderLength +
"\r\nIP checksum: " + ipheader.headerChecksum +
"\r\nMay fragement? " + ipheader.isMayFragment +
"\r\nLast fragment? " + ipheader.isLastFragment +
"\r\nLast fragment? " + ipheader.lastFragment +
"\r\nFlag: " + ipheader.flag +
"\r\nFragment Offset: " + ipheader.fragmentOffset +
"\r\nDest: " + intToIPAddress(ipheader.destinationIP) +
Expand Down Expand Up @@ -346,7 +344,7 @@ object PacketUtil {
.append("\r\nTCP Checksum: ").append(tcpheader.checksum)
.append("\r\nIs Valid TCP checksum: ").append(isValidChecksum)
.append("\r\nMay fragement? ").append(ipHeader.isMayFragment)
.append("\r\nLast fragment? ").append(ipHeader.isLastFragment)
.append("\r\nLast fragment? ").append(ipHeader.lastFragment)
.append("\r\nFlag: ").append(ipHeader.flag.toInt())
.append("\r\nFragment Offset: ").append(ipHeader.fragmentOffset.toInt())
.append("\r\nWindow: ").append(tcpheader.windowSize)
Expand Down

0 comments on commit 09707d3

Please sign in to comment.