Skip to content

Commit

Permalink
fix: spawn ENAMETOOLONG by reduceTrackerString
Browse files Browse the repository at this point in the history
  • Loading branch information
agalwood committed Jun 15, 2021
1 parent 333f01a commit 1de2acc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/main/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
import { checkIsNeedRun } from '@shared/utils'
import {
convertTrackerDataToComma,
fetchBtTrackerFromSource
fetchBtTrackerFromSource,
reduceTrackerString
} from '@shared/utils/tracker'
import logger from './core/Logger'
import ConfigManager from './core/ConfigManager'
Expand Down Expand Up @@ -316,7 +317,8 @@ export default class Application extends EventEmitter {
return
}

const tracker = convertTrackerDataToComma(data)
let tracker = convertTrackerDataToComma(data)
tracker = reduceTrackerString(tracker)
this.savePreference({
system: {
'bt-tracker': tracker
Expand Down
5 changes: 5 additions & 0 deletions src/main/core/ConfigManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
NGOSANG_TRACKERS_BEST_URL_CDN
} from '@shared/constants'
import { separateConfig } from '@shared/utils'
import { reduceTrackerString } from '@shared/utils/tracker'

export default class ConfigManager {
constructor () {
Expand Down Expand Up @@ -140,6 +141,10 @@ export default class ConfigManager {
Object.keys(others).forEach(key => {
this.systemConfig.delete(key)
})

// Fix spawn ENAMETOOLONG on Windows
const tracker = reduceTrackerString(this.systemConfig.get('bt-tracker'))
this.setSystemConfig('bt-tracker', tracker)
}

fixUserConfig () {
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/components/Preference/Advanced.vue
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@
diffConfig,
getRandomInt
} from '@shared/utils'
import { convertTrackerDataToLine } from '@shared/utils/tracker'
import { convertTrackerDataToLine, reduceTrackerString } from '@shared/utils/tracker'
import '@/components/Icons/dice'
import '@/components/Icons/sync'
import '@/components/Icons/refresh'
Expand Down Expand Up @@ -613,7 +613,7 @@
const { btTracker, noProxy } = changed
if (btTracker) {
data.btTracker = convertLineToComma(btTracker)
data.btTracker = reduceTrackerString(convertLineToComma(btTracker))
}
if (noProxy) {
Expand Down
2 changes: 2 additions & 0 deletions src/shared/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export const ENGINE_RPC_PORT = 16800
export const UNKNOWN_PEERID = '%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00'
export const GRAPHIC = '░▒▓█'

export const MAX_BT_TRACKER_LENGTH = 6144

/**
* @see https://github.com/ngosang/trackerslist
*/
Expand Down
12 changes: 12 additions & 0 deletions src/shared/utils/tracker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isEmpty } from 'lodash'
import axios from 'axios'
import { MAX_BT_TRACKER_LENGTH } from '@shared/constants'

export const fetchBtTrackerFromSource = async (source) => {
if (isEmpty(source)) {
Expand All @@ -26,3 +27,14 @@ export function convertTrackerDataToComma (arr = []) {
const result = convertTrackerDataToLine(arr).replace(/(?:\r\n|\r|\n)/g, ',').trim()
return result
}

export function reduceTrackerString (str = '') {
if (str.length <= MAX_BT_TRACKER_LENGTH) {
return str
}

const subStr = str.substring(0, MAX_BT_TRACKER_LENGTH)
const index = subStr.lastIndexOf(',')
const result = subStr.substring(0, index)
return result
}

0 comments on commit 1de2acc

Please sign in to comment.